Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: rpc internal message #6

Merged
merged 1 commit into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/errors/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export class RpcErrorHandler implements ErrorHandler {
public handle(_data: string, { error }: ErrorHandlerErrorInfo): DecodedError {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const rpcError = error as any
return rpcErrorResult({ data: null, name: rpcError.code, reason: rpcError.message })
const reason = rpcError.info?.error?.message ?? rpcError.shortMessage ?? rpcError.message
return rpcErrorResult({ data: null, name: rpcError.code, reason })
}
}
100 changes: 76 additions & 24 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,36 +569,88 @@ describe('ErrorDecoder', () => {
})

describe('When reverted not due to contract errors', () => {
beforeEach(async () => {
try {
await contract.revertWithReason('Test message', {
gasLimit: 100000,
gasPrice: '1180820112192848923743894728934',
})
expect.fail('Expected to revert')
} catch (e) {
decodedError = await errorDecoder.decode(e)
}
})
describe('When decoding RPC errors', () => {
beforeEach(async () => {
try {
await contract.revertWithReason('Test message', {
gasLimit: 100000,
gasPrice: '1180820112192848923743894728934',
})
expect.fail('Expected to revert')
} catch (e) {
decodedError = await errorDecoder.decode(e)
}
})

it('should return error type as RpcError', async () => {
expect(decodedError.type).to.equal(ErrorType.RpcError)
})
it('should return error type as RpcError', async () => {
expect(decodedError.type).to.equal(ErrorType.RpcError)
})

it('should return error code as name', async () => {
expect(decodedError.name).to.equal('-32000')
})
it('should return error code as name', async () => {
expect(decodedError.name).to.equal('-32000')
})

it('should return the error reason', async () => {
expect(decodedError.reason).to.contain("sender doesn't have enough funds to send tx")
})
it('should return the error reason', async () => {
expect(decodedError.reason).to.contain("sender doesn't have enough funds to send tx")
})

it('should return data as null', async () => {
expect(decodedError.data).to.be.null
it('should return data as null', async () => {
expect(decodedError.data).to.be.null
})

it('should return empty args', async () => {
expect(decodedError.args.length).to.equal(0)
})
})

it('should return empty args', async () => {
expect(decodedError.args.length).to.equal(0)
describe('When obtaining the error message in RPC errors', () => {
const FakeRpcError = class extends Error {
public constructor(
public code?: string,
public info?: { error: { code: number; message: string } },
public shortMessage?: string,
) {
super('Fake error object message')
}
}

let fakeRpcError = new FakeRpcError()

beforeEach(async () => {
fakeRpcError = new FakeRpcError(
'INSUFFICIENT_FUNDS',
{
error: {
code: -32000,
message: 'insufficient funds for gas * price + value: balance 0',
},
},
'insufficient funds for intrinsic transaction cost',
)
})

it('should use long error message if exists', async () => {
decodedError = await errorDecoder.decode(fakeRpcError)

expect(decodedError.reason).to.equal(fakeRpcError.info.error.message)
})

it('should use short message if long error message does not exist', async () => {
fakeRpcError.info = undefined

decodedError = await errorDecoder.decode(fakeRpcError)

expect(decodedError.reason).to.equal(fakeRpcError.shortMessage)
})

it('should fallback to using message in error object', async () => {
fakeRpcError.info = undefined
fakeRpcError.shortMessage = undefined

decodedError = await errorDecoder.decode(fakeRpcError)

expect(decodedError.reason).to.equal(fakeRpcError.message)
})
})
})

Expand Down
Loading