-
Notifications
You must be signed in to change notification settings - Fork 376
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Guilhem Fanton <guilhem.fanton@gmail.com>
- Loading branch information
Showing
2 changed files
with
99 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { errcode, account } from '@berty-tech/api' | ||
import { reduce } from 'lodash' | ||
|
||
class GRPCError extends Error { | ||
public EOF: boolean | ||
public OK: boolean | ||
public Code: errcode.ErrCode | ||
public GrpcCode: account.GRPCErrCode | ||
|
||
private error: account.Error | ||
|
||
constructor(e: account.IError | null | undefined) { | ||
if (!e) { | ||
// this should not happen, but should not break the app either. | ||
// instead simply create a empty error and warn about this | ||
console.warn(`GRPCError: (${e}) grpc error provided, empty error returned`) | ||
e = account.Error.create({}) | ||
} | ||
|
||
const error = account.Error.create(e) | ||
super(error.message) | ||
|
||
this.error = error | ||
this.Code = error.errorCode | ||
this.GrpcCode = error.grpcErrorCode | ||
|
||
this.OK = | ||
error.grpcErrorCode === account.GRPCErrCode.OK && | ||
error.errorCode === errcode.ErrCode.Undefined | ||
this.EOF = | ||
error.grpcErrorCode === account.GRPCErrCode.CANCELED || | ||
(error.grpcErrorCode === account.GRPCErrCode.UNKNOWN && error.message === 'EOF') | ||
} | ||
|
||
public details(): errcode.ErrDetails { | ||
if (this.error.errorDetails) { | ||
return errcode.ErrDetails.create(this.error.errorDetails) | ||
} | ||
|
||
return errcode.ErrDetails.create({}) | ||
} | ||
|
||
public errCode(): errcode.ErrCode { | ||
return this.Code | ||
} | ||
|
||
public grpcErrorCode(): account.GRPCErrCode { | ||
return this.GrpcCode | ||
} | ||
|
||
public hasErrCode(error: errcode.ErrCode): boolean { | ||
return reduce(this.error.errorDetails?.codes, (ac, v) => ac && v == error, false) || false | ||
} | ||
} | ||
|
||
export default GRPCError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters