This repository has been archived by the owner on Aug 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(shared): combine BackendError with APIError
- Loading branch information
1 parent
098e84c
commit 2657408
Showing
9 changed files
with
128 additions
and
89 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,16 +1,73 @@ | ||
import { AxiosError } from 'axios'; | ||
import { IOError } from 'ts-io-error/lib'; | ||
|
||
export const isAPIError = (e: unknown): e is APIError => { | ||
return (e as any).name === 'APIError'; | ||
}; | ||
|
||
export class APIError extends Error { | ||
export class APIError extends IOError { | ||
public readonly name = 'APIError'; | ||
} | ||
|
||
export const NotAuthorizedError = (): APIError => { | ||
return { | ||
name: 'APIError', | ||
status: 401, | ||
message: 'Authorization header is missing', | ||
details: { | ||
kind: 'ClientError', | ||
status: '401', | ||
}, | ||
}; | ||
}; | ||
|
||
constructor( | ||
public readonly status: number, | ||
public readonly type: string, | ||
public readonly message: string, | ||
public readonly details: string[] | ||
) { | ||
super(message); | ||
export const NotFoundError = (resource: string): APIError => { | ||
return { | ||
name: 'APIError', | ||
status: 404, | ||
message: `Can't find the resource ${resource}`, | ||
details: { | ||
kind: 'ClientError', | ||
status: '404', | ||
}, | ||
}; | ||
}; | ||
|
||
export const fromAxiosError = (e: AxiosError): APIError => { | ||
return ( | ||
e.response?.data ?? { | ||
name: 'APIError', | ||
status: e.response?.status ?? 500, | ||
message: e.message, | ||
details: { | ||
kind: 'ServerError', | ||
status: '500', | ||
meta: e.response?.data, | ||
}, | ||
} | ||
); | ||
}; | ||
|
||
export const toAPIError = (e: unknown): APIError => { | ||
// eslint-disable-next-line | ||
console.error(e); | ||
if (e instanceof APIError) { | ||
return new APIError(e.message, { | ||
kind: 'ServerError', | ||
status: e.status + '', | ||
meta: e.details, | ||
}); | ||
} | ||
} | ||
|
||
if (e instanceof Error) { | ||
return new APIError(e.message, { | ||
kind: 'ServerError', | ||
status: e.name, | ||
}); | ||
} | ||
|
||
return new APIError('UnknownError', { | ||
kind: 'ServerError', | ||
status: '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
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 |
---|---|---|
@@ -1,14 +1,24 @@ | ||
import { failure } from 'io-ts/lib/PathReporter'; | ||
import { APIError, isAPIError } from './APIError'; | ||
import * as t from 'io-ts'; | ||
|
||
export const toValidationError = ( | ||
message: string, | ||
details: string[] | ||
errors: t.ValidationError[] | ||
): APIError => { | ||
return new APIError(400, 'ValidationError', message, details); | ||
return { | ||
name: 'APIError', | ||
message, | ||
status: 400, | ||
details: { | ||
kind: 'DecodingError', | ||
errors: failure(errors), | ||
}, | ||
}; | ||
}; | ||
|
||
export const isValidationError = ( | ||
e: unknown | ||
): e is APIError & { type: 'ValidationError' } => { | ||
return isAPIError(e) && e.type === 'ValidationError'; | ||
return isAPIError(e) && e.details.kind === 'DecodingError'; | ||
}; |
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