Skip to content

Commit

Permalink
fix(Provider): errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Nov 12, 2024
1 parent 806207f commit 33b5123
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/fifty-ears-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ox": patch
---

Updated Provider errors.
36 changes: 23 additions & 13 deletions src/core/Provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,44 +92,54 @@ export type EventMap = {
/** The user rejected the request. */
export class UserRejectedRequestError extends ProviderRpcError {
static readonly code = 4001
override readonly code = 4001
override readonly name = 'Provider.UserRejectedRequestError'
override readonly message = 'The user rejected the request.'

constructor() {
super(4001, 'The user rejected the request.')
}
}

/** The requested method and/or account has not been authorized by the user. */
export class UnauthorizedError extends ProviderRpcError {
static readonly code = 4100
override readonly code = 4100
override readonly name = 'Provider.UnauthorizedError'
override readonly message =
'The requested method and/or account has not been authorized by the user.'

constructor() {
super(
4100,
'The requested method and/or account has not been authorized by the user.',
)
}
}

/** The provider does not support the requested method. */
export class UnsupportedMethodError extends ProviderRpcError {
static readonly code = 4200
override readonly code = 4200
override readonly name = 'Provider.UnsupportedMethodError'
override readonly message =
'The provider does not support the requested method.'

constructor() {
super(4200, 'The provider does not support the requested method.')
}
}

/** The provider is disconnected from all chains. */
export class DisconnectedError extends ProviderRpcError {
static readonly code = 4900
override readonly code = 4900
override readonly name = 'Provider.DisconnectedError'
override readonly message = 'The provider is disconnected from all chains.'

constructor() {
super(4900, 'The provider is disconnected from all chains.')
}
}

/** The provider is not connected to the requested chain. */
export class ChainDisconnectedError extends ProviderRpcError {
static readonly code = 4901
override readonly code = 4901
override readonly name = 'Provider.ChainDisconnectedError'
override readonly message =
'The provider is disconnected from the requested chain.'

constructor() {
super(4901, 'The provider is not connected to the requested chain.')
}
}

/**
Expand Down
34 changes: 32 additions & 2 deletions src/core/_test/Provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, expect, test } from 'vitest'
import { anvilMainnet } from '../../../test/anvil.js'
import { address } from '../../../test/constants/addresses.js'

describe('createEmitter', () => {
describe('Provider.createEmitter', () => {
test('default', () => {
const emitter = Provider.createEmitter()

Expand All @@ -24,7 +24,7 @@ describe('createEmitter', () => {
})
})

describe('from', () => {
describe('Provider.from', () => {
test('default', async () => {
const store = RpcRequest.createStore()

Expand Down Expand Up @@ -114,6 +114,36 @@ describe('from', () => {
})
})

test('Provider.InvalidMessageFieldError', () => {
expect(new Provider.UserRejectedRequestError()).toMatchInlineSnapshot(
'[Provider.UserRejectedRequestError: The user rejected the request.]',
)
})

test('Provider.UnauthorizedError', () => {
expect(new Provider.UnauthorizedError()).toMatchInlineSnapshot(
'[Provider.UnauthorizedError: The requested method and/or account has not been authorized by the user.]',
)
})

test('Provider.UnsupportedMethodError', () => {
expect(new Provider.UnsupportedMethodError()).toMatchInlineSnapshot(
'[Provider.UnsupportedMethodError: The provider does not support the requested method.]',
)
})

test('Provider.DisconnectedError', () => {
expect(new Provider.DisconnectedError()).toMatchInlineSnapshot(
'[Provider.DisconnectedError: The provider is disconnected from all chains.]',
)
})

test('Provider.ChainDisconnectedError', () => {
expect(new Provider.ChainDisconnectedError()).toMatchInlineSnapshot(
'[Provider.ChainDisconnectedError: The provider is not connected to the requested chain.]',
)
})

test('exports', () => {
expect(Object.keys(Provider)).toMatchInlineSnapshot(`
[
Expand Down

0 comments on commit 33b5123

Please sign in to comment.