forked from berty/weshnet-expo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.ts
85 lines (68 loc) · 2.31 KB
/
error.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { reduce } from 'lodash'
import api from './api'
import types from './api/index.d'
class GRPCError extends Error {
public EOF: boolean
public OK: boolean
// public Code: beerrcode.ErrCode | beweshnet_errcode.ErrCode
public GrpcCode: types.rpcmanager.GRPCErrCode
public error: types.rpcmanager.Error
constructor(e: types.rpcmanager.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 = api.rpcmanager.Error.create({})
}
const error = api.rpcmanager.Error.create(e)
super(error.message)
this.error = error
// this.Code = error.errorCode
this.GrpcCode = error.grpcErrorCode
this.OK = error.grpcErrorCode === api.rpcmanager.GRPCErrCode.OK
// error.errorCode === beerrcode.ErrCode.Undefined
this.EOF =
error.grpcErrorCode === api.rpcmanager.GRPCErrCode.CANCELED ||
(error.grpcErrorCode === api.rpcmanager.GRPCErrCode.UNKNOWN && error.message === 'EOF')
}
// public details(): beerrcode.ErrDetails {
// if (this.error.errorDetails) {
// return beerrcode.ErrDetails.create(this.error.errorDetails)
// }
// return beerrcode.ErrDetails.create({})
// }
// public errCode(): beerrcode.ErrCode | beweshnet_errcode.ErrCode {
// return this.Code
// }
public grpcErrorCode(): types.rpcmanager.GRPCErrCode {
return this.GrpcCode
}
public toJSON(): any {
// const details = this.details().codes.map((err: beerrcode.ErrCode) => {
// return beerrcode.ErrCode[err]
// })
return {
message: this.message,
grpcErrorCode: api.rpcmanager.GRPCErrCode[this.GrpcCode],
// errorCode: beerrcode.ErrCode[this.Code],
// details: details,
EOF: this.EOF,
OK: this.OK,
}
}
// public hasErrCode(error: beerrcode.ErrCode): boolean {
// return reduce(this.error.errorDetails?.codes, (ac, v) => ac && v == error, false) || false
// }
}
const newGRPCError = (code: number, message: string): GRPCError => {
const error = api.rpcmanager.Error.fromObject({
message: message,
grpcErrorCode: code,
})
return new GRPCError(error)
}
const EOF = new GRPCError({
grpcErrorCode: api.rpcmanager.GRPCErrCode.CANCELED,
message: 'EOF',
})
export { GRPCError, EOF, newGRPCError }