Skip to content

Commit

Permalink
refactor(exchage): moving to package
Browse files Browse the repository at this point in the history
  • Loading branch information
stackchain committed Mar 16, 2024
1 parent 6341a3c commit 6f89046
Show file tree
Hide file tree
Showing 27 changed files with 661 additions and 305 deletions.
4 changes: 2 additions & 2 deletions apps/wallet-mobile/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ PODS:
- react-native-config/App (= 1.5.1)
- react-native-config/App (1.5.1):
- React-Core
- react-native-haskell-shelley (6.0.0-alpha.3):
- react-native-haskell-shelley (6.0.0-alpha.4):
- React
- react-native-mmkv (2.11.0):
- MMKV (>= 1.2.13)
Expand Down Expand Up @@ -810,7 +810,7 @@ SPEC CHECKSUMS:
react-native-background-timer: 17ea5e06803401a379ebf1f20505b793ac44d0fe
react-native-ble-plx: f10240444452dfb2d2a13a0e4f58d7783e92d76e
react-native-config: 86038147314e2e6d10ea9972022aa171e6b1d4d8
react-native-haskell-shelley: 2b098fcb8072f107d464026f0f7f389740fb46ed
react-native-haskell-shelley: 46f5fa9068996637c969e9526c22bd3867c27810
react-native-mmkv: e97c0c79403fb94577e5d902ab1ebd42b0715b43
react-native-pager-view: 0ccb8bf60e2ebd38b1f3669fa3650ecce81db2df
react-native-quick-base64: 62290829c619fbabca4c41cfec75ae759d08fc1c
Expand Down
155 changes: 155 additions & 0 deletions packages/common/src/api/getApiError.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import {getApiError} from './getApiError'
import {Api} from '@yoroi/types'

describe('getApiError', () => {
it('should throw NetworkError for -1 status', () => {
expect(() => {
const error = getApiError({
status: -1,
message: 'Network error',
responseData: null,
})
throw error
}).toThrow(Api.Errors.Network)
})

it('should throw InvalidStateError for -2 status', () => {
expect(() => {
const error = getApiError({
status: -2,
message: 'Invalid state',
responseData: null,
})
throw error
}).toThrow(Api.Errors.InvalidState)
})

it('should throw BadRequestError for 400 status', () => {
expect(() => {
const error = getApiError({
status: 400,
message: 'Bad request',
responseData: null,
})
throw error
}).toThrow(Api.Errors.BadRequest)
})

it('should throw UnauthorizedError for 401 status', () => {
expect(() => {
const error = getApiError({
status: 401,
message: 'Unauthorized',
responseData: null,
})
throw error
}).toThrow(Api.Errors.Unauthorized)
})

it('should throw ForbiddenError for 403 status', () => {
expect(() => {
const error = getApiError({
status: 403,
message: 'Forbidden',
responseData: null,
})
throw error
}).toThrow(Api.Errors.Forbidden)
})

it('should throw NotFoundError for 404 status', () => {
expect(() => {
const error = getApiError({
status: 404,
message: 'Not found',
responseData: null,
})
throw error
}).toThrow(Api.Errors.NotFound)
})

it('should throw ConflictError for 409 status', () => {
expect(() => {
const error = getApiError({
status: 409,
message: 'Conflict',
responseData: null,
})
throw error
}).toThrow(Api.Errors.Conflict)
})

it('should throw GoneError for 410 status', () => {
expect(() => {
const error = getApiError({
status: 410,
message: 'Gone',
responseData: null,
})
throw error
}).toThrow(Api.Errors.Gone)
})

it('should throw TooEarlyError for 425 status', () => {
expect(() => {
const error = getApiError({
status: 425,
message: 'Too early',
responseData: null,
})
throw error
}).toThrow(Api.Errors.TooEarly)
})

it('should throw TooManyRequestsError for 429 status', () => {
expect(() => {
const error = getApiError({
status: 429,
message: 'Too many requests',
responseData: null,
})
throw error
}).toThrow(Api.Errors.TooManyRequests)
})

it('should throw ServerSideError for 500 status', () => {
expect(() => {
const error = getApiError({
status: 500,
message: 'Server error',
responseData: null,
})
throw error
}).toThrow(Api.Errors.ServerSide)
})

it('should throw ServerSideError for other 5xx status codes', () => {
expect(() => {
const error = getApiError({
status: 503,
message: 'Service unavailable',
responseData: null,
})
throw error
}).toThrow(Api.Errors.ServerSide)
expect(() => {
const error = getApiError({
status: 504,
message: 'Gateway timeout',
responseData: null,
})
throw error
}).toThrow(Api.Errors.ServerSide)
})

it('should throw UnknownError for unhandled status codes', () => {
expect(() => {
const error = getApiError({
status: 999,
message: 'Unknown error',
responseData: null,
})
throw error
}).toThrow(Api.Errors.Unknown)
})
})
32 changes: 32 additions & 0 deletions packages/common/src/api/getApiError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {Api} from '@yoroi/types'

export const getApiError = (error: Api.ResponseError) => {
if (error.status >= 500 && error.status < 600) {
return new Api.Errors.ServerSide(error.message)
}

switch (error.status) {
case -1:
return new Api.Errors.Network(error.message)
case -2:
return new Api.Errors.InvalidState(error.message)
case 400:
return new Api.Errors.BadRequest(error.message)
case 401:
return new Api.Errors.Unauthorized(error.message)
case 403:
return new Api.Errors.Forbidden(error.message)
case 404:
return new Api.Errors.NotFound(error.message)
case 409:
return new Api.Errors.Conflict(error.message)
case 410:
return new Api.Errors.Gone(error.message)
case 425:
return new Api.Errors.TooEarly(error.message)
case 429:
return new Api.Errors.TooManyRequests(error.message)
default:
return new Api.Errors.Unknown(error.message)
}
}
117 changes: 0 additions & 117 deletions packages/common/src/api/handleApiError.test.ts

This file was deleted.

32 changes: 0 additions & 32 deletions packages/common/src/api/handleApiError.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export * from './storage/translators/sync-storage-reactjs'
export * from './storage/translators/async-storage-reactjs'
export * from './api/fetcher'
export * from './api/fetchData'
export * from './api/handleApiError'
export * from './api/getApiError'
export * from './observer/observer'
export * from './translators/reactjs/hooks/useMutationWithInvalidations'
export * from './helpers/monads'
Expand Down
2 changes: 2 additions & 0 deletions packages/exchange/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
"eslint-plugin-ft-flow": "^3.0.0",
"eslint-plugin-prettier": "^4.0.0",
"flowgen": "^1.21.0",
"immer": "^10.0.2",
"jest": "^28.1.1",
"pod-install": "^0.1.0",
"prettier": "^2.0.5",
Expand All @@ -163,6 +164,7 @@
"peerDependencies": {
"@react-native-async-storage/async-storage": ">= 1.19.3 <= 1.20.0",
"axios": "^1.5.0",
"immer": "^10.0.2",
"react": ">= 16.8.0 <= 19.0.0",
"react-query": "^3.39.3",
"zod": "^3.22.4"
Expand Down
Loading

0 comments on commit 6f89046

Please sign in to comment.