-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(exchage): moving to package
- Loading branch information
1 parent
6341a3c
commit 6f89046
Showing
27 changed files
with
661 additions
and
305 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
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) | ||
}) | ||
}) |
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,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) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.