firebase / firebase/firebase-admin-node

FirebaseError type definition as class instead of interface

Đang mở
#403 21 bình luận 49 reaction 0 người được giao Xem trên GitHub
Ngôn ngữ chính
TypeScript
Star
1.7k
Fork
419
Merge trung bình
3 ngày 10 giờ
Pull request đã merge (30 ngày)
16

Mô tả

### Description

Currently is `FirebaseError` defined as interface in typescript definition file. So in case of `async/await` we have to handle errors checking e.g. `code` property existence:
```
try {
const decodedIdToken = await this.auth.verifyIdToken(firebaseIdToken);
// some another calls async methods
} catch(err) {
if (err.code === 'auth/argument-error') {
// handle errors - but err is still any type
}
}
```
This is proper way to handle errors but a little bit cleaner way would be:
```
try {
const decodedIdToken = await this.auth.verifyIdToken(firebaseIdToken);
// some another calls async methods
} catch(err) {
if (err instanceof FirebaseError) {
// handle error codes and proper typing here
}
}
```
It is just a small improvement but in this way we have correct type in if statement and also `FirebaseError.code` would be some string literal type with defined all possible error codes.
```
type FirebaseErrorCode = 'auth/argument-error' | 'auth/id-token-expired' | ... ;

class FirebaseError extends Error {
code: FirebaseErrorCode;
}
```
I think this could be really helpful in error handling.

### Target environment

* Operating System version: all
* Firebase SDK version: latest
* Library version: latest
* Firebase Product: common

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.