-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #453 from nervosnetwork/refactor-internal-exceptions
refactor: refactor internal exceptions
- Loading branch information
Showing
38 changed files
with
666 additions
and
134 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
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,74 @@ | ||
{ | ||
"IdNotMatchException": { | ||
"params": [10000, 10001], | ||
"expected": { | ||
"code": 201, | ||
"message": "Expect json rpc id to be 10000, but 10001 received" | ||
} | ||
}, | ||
"ResponseException": { | ||
"params": ["Response_Err_Message"], | ||
"expected": { | ||
"code": 204, | ||
"message": "Response_Err_Message" | ||
} | ||
}, | ||
"PageSizeTooLargeException": { | ||
"params": [51, 50], | ||
"expected": { | ||
"code": 101, | ||
"message": "Expect page size to be at most 50, but 51 received" | ||
} | ||
}, | ||
"PageSizeTooSmallException": { | ||
"params": [-1, 0], | ||
"expected": { | ||
"code": 101, | ||
"message": "Expect page size to be at least 0, but -1 received" | ||
} | ||
}, | ||
"OutputsValidatorTypeException": { | ||
"params": [], | ||
"expected": { | ||
"code": 101, | ||
"message": "Expect outputs validator to be 'default' or 'passthrough'" | ||
} | ||
}, | ||
"BigintOrHexStringTypeException": { | ||
"params": ["ab"], | ||
"expected": { | ||
"code": 101, | ||
"message": "Expect number to be bigint or hex string, but ab received" | ||
} | ||
}, | ||
"StringHashTypeException": { | ||
"params": [1], | ||
"expected": { | ||
"code": 101, | ||
"message": "Expect hash to be string, but 1 received" | ||
} | ||
}, | ||
"MethodInBatchNotFoundException": { | ||
"params": ["Method_Name"], | ||
"expected": { | ||
"code": 202, | ||
"message": "[Batch Request]: Method Method_Name is not found" | ||
} | ||
}, | ||
"PayloadInBatchException": { | ||
"params": [1, "Payload_Err_Message"], | ||
"expected": { | ||
"code": 203, | ||
"index": 1, | ||
"message": "[Batch Request 1]: Payload_Err_Message" | ||
} | ||
}, | ||
"IdNotMatchedInBatchException": { | ||
"params": [1, 10000, 10001], | ||
"expected": { | ||
"code": 201, | ||
"index": 1, | ||
"message": "[Batch Request 1]: Expect json rpc id to be 10000, but 10001 received" | ||
} | ||
} | ||
} |
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,18 @@ | ||
const exceptions = require('../../lib/exceptions') | ||
const fixtures = require('./fixtures.json') | ||
|
||
describe('Test exceptions', () => { | ||
const fixtureTable = Object.entries(fixtures).map(([exceptionName, { params, expected }]) => [ | ||
exceptionName, | ||
params, | ||
expected, | ||
]) | ||
test.each(fixtureTable)(`%s`, (exceptionName, params, expected) => { | ||
const err = new exceptions[exceptionName](...params) | ||
expect(err.code).toBe(expected.code) | ||
expect(err.message).toBe(expected.message) | ||
if (err.index) { | ||
expect(err.index).toBe(expected.index) | ||
} | ||
}) | ||
}) |
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,8 @@ | ||
export enum ErrorCode { | ||
IdNotMatch = 201, | ||
MethodNotFound = 202, | ||
PayloadMessage = 203, | ||
ResponseMessage = 204, | ||
} | ||
|
||
export default ErrorCode |
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,39 @@ | ||
import ErrorCode from './ErrorCode' | ||
import { IdNotMatchException } from './rpc' | ||
|
||
const ERROR_LABEL = 'Batch Request' | ||
|
||
export class MethodInBatchNotFoundException extends Error { | ||
code = ErrorCode.MethodNotFound | ||
|
||
constructor(name: string) { | ||
super(`[${ERROR_LABEL}]: Method ${name} is not found`) | ||
} | ||
} | ||
|
||
export class PayloadInBatchException extends Error { | ||
code = ErrorCode.PayloadMessage | ||
|
||
index: number | undefined | ||
|
||
constructor(index: number, message: string) { | ||
super(`[${ERROR_LABEL} ${index}]: ${message}`) | ||
this.index = index | ||
} | ||
} | ||
|
||
export class IdNotMatchedInBatchException extends IdNotMatchException { | ||
index: number | undefined | ||
|
||
constructor(index: number, requestId: number, responseId: number) { | ||
super(requestId, responseId) | ||
this.message = `[${ERROR_LABEL} ${index}]: ${this.message}` | ||
this.index = index | ||
} | ||
} | ||
|
||
export default { | ||
MethodInBatchNotFoundException, | ||
PayloadInBatchException, | ||
IdNotMatchedInBatchException, | ||
} |
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,49 @@ | ||
import { ErrorCode } from '@nervosnetwork/ckb-sdk-utils/lib/exceptions' | ||
|
||
export class PageSizeTooLargeException extends RangeError { | ||
code = ErrorCode.ParameterInvalid | ||
|
||
constructor(pageSize: bigint | string, maxSize: number) { | ||
super(`Expect page size to be at most ${maxSize}, but ${pageSize} received`) | ||
} | ||
} | ||
|
||
export class PageSizeTooSmallException extends RangeError { | ||
code = ErrorCode.ParameterInvalid | ||
|
||
constructor(pageSize: bigint | string, minSize: number) { | ||
super(`Expect page size to be at least ${minSize}, but ${pageSize} received`) | ||
} | ||
} | ||
|
||
export class OutputsValidatorTypeException extends TypeError { | ||
code = ErrorCode.ParameterInvalid | ||
|
||
constructor() { | ||
super(`Expect outputs validator to be 'default' or 'passthrough'`) | ||
} | ||
} | ||
|
||
export class BigintOrHexStringTypeException extends TypeError { | ||
code = ErrorCode.ParameterInvalid | ||
|
||
constructor(value: any) { | ||
super(`Expect number to be bigint or hex string, but ${value} received`) | ||
} | ||
} | ||
|
||
export class StringHashTypeException extends TypeError { | ||
code = ErrorCode.ParameterInvalid | ||
|
||
constructor(hash: any) { | ||
super(`Expect hash to be string, but ${hash} received`) | ||
} | ||
} | ||
|
||
export default { | ||
PageSizeTooLargeException, | ||
PageSizeTooSmallException, | ||
OutputsValidatorTypeException, | ||
BigintOrHexStringTypeException, | ||
StringHashTypeException, | ||
} |
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,4 @@ | ||
export { HexStringWithout0xException } from '@nervosnetwork/ckb-sdk-utils/lib/exceptions' | ||
export * from './formatter' | ||
export * from './rpc' | ||
export * from './batch' |
Oops, something went wrong.