-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved/altered error codec & added PFS errors
- Loading branch information
Showing
8 changed files
with
130 additions
and
66 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
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
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,12 @@ | ||
export enum PfsErrorCodes { | ||
PFS_EMPTY_URL = 'A registered Pathfinding Service returned an empty service URL.', | ||
PFS_INVALID_URL = 'A registered Pathfinding Service returned an invalid service URL.', | ||
PFS_INVALID_INFO = 'Could not any valid Pathfinding services. Client and PFS versions are possibly out-of-sync.', | ||
PFS_NO_ROUTES_FOUND = 'No valid routes found.', | ||
PFS_ERROR_RESPONSE = 'Pathfinding Service request returned an error', | ||
PFS_DISABLED = 'Pathfinding Service is disabled and no direct route is available.', | ||
PFS_UNKNOWN_TOKEN_NETWORK = 'Unknown token network.', | ||
PFS_TARGET_OFFLINE = 'The requested target is offline.', | ||
PFS_LAST_IOU_REQUEST_FAILED = 'The request for the last IOU has failed.', | ||
PFS_IOU_SIGNATURE_MISMATCH = 'The signature of the last IOU did not match.', | ||
} |
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
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
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 |
---|---|---|
@@ -1,26 +1,88 @@ | ||
export type ErrorDetail = { [key: string]: string }; | ||
import * as t from 'io-ts'; | ||
import { map } from 'fp-ts/lib/Either'; | ||
import { pipe } from 'fp-ts/lib/pipeable'; | ||
|
||
export default abstract class RaidenError extends Error { | ||
import { PfsErrorCodes } from '../path/errors'; | ||
|
||
export const ErrorDetails = t.array( | ||
t.type({ | ||
key: t.string, | ||
value: t.union([t.string, t.number]), | ||
}), | ||
); | ||
export interface ErrorDetails extends t.TypeOf<typeof ErrorDetails> {} | ||
|
||
export default class RaidenError extends Error { | ||
code: string; | ||
details?: ErrorDetail[]; | ||
details?: ErrorDetails; | ||
|
||
constructor(message: string, detail?: ErrorDetail[] | ErrorDetail) { | ||
constructor(message: string, details?: ErrorDetails) { | ||
super(message || 'General Error'); | ||
this.name = 'RaidenError'; | ||
this.code = this.getCode(message); | ||
this.details = details; | ||
} | ||
|
||
if (detail) { | ||
this.details = Array.isArray(detail) ? detail : [detail]; | ||
} | ||
getCode(message: string): string { | ||
return message ?? 'RAIDEN_ERROR'; | ||
} | ||
} | ||
|
||
addDetail(detail: ErrorDetail) { | ||
if (this.details) { | ||
this.details.push(detail); | ||
} else { | ||
this.details = [detail]; | ||
} | ||
export class PfsError extends RaidenError { | ||
constructor(message: PfsErrorCodes, details?: ErrorDetails) { | ||
super(message, details); | ||
this.name = 'PfsError'; | ||
} | ||
|
||
abstract getCode(message: string): string; | ||
getCode(message: string): string { | ||
return ( | ||
Object.keys(PfsErrorCodes).find(code => Object(PfsErrorCodes)[code] === message) ?? | ||
'PFS_GENERAL_ERROR' | ||
); | ||
} | ||
} | ||
|
||
const serializedErr = t.intersection([ | ||
t.type({ name: t.string, message: t.string, code: t.string }), | ||
t.partial({ stack: t.string, details: ErrorDetails }), | ||
]); | ||
|
||
/** | ||
* Simple Error codec | ||
* | ||
* This codec doesn't decode to an instance of the exact same error class object, but instead to | ||
* a generic Error, but assigning 'name', 'stack' & 'message' properties, more as an informative | ||
* object. | ||
*/ | ||
export const ErrorCodec = new t.Type< | ||
RaidenError, | ||
{ name: string; message: string; code: string; stack?: string; details?: ErrorDetails } | ||
>( | ||
'RaidenError', | ||
(u: unknown): u is RaidenError => u instanceof RaidenError, | ||
u => { | ||
if (u instanceof RaidenError) return t.success(u); | ||
return pipe( | ||
serializedErr.decode(u), | ||
map(({ name, message, code, stack, details }) => { | ||
switch (name) { | ||
case 'PfsError': | ||
return Object.assign(new PfsError(message as PfsErrorCodes, details), { | ||
name, | ||
stack, | ||
code, | ||
}); | ||
} | ||
|
||
return Object.assign(new RaidenError(message), { name, stack }); | ||
}), | ||
); | ||
}, | ||
({ name, message, stack, details, code }) => ({ | ||
name, | ||
message, | ||
stack, | ||
code, | ||
details: details ?? undefined, | ||
}), | ||
); |
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
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