This repository has been archived by the owner on Nov 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix typing, api error handling, and update tests
- Loading branch information
1 parent
64c3895
commit 6ad63dc
Showing
9 changed files
with
62 additions
and
25 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
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
const getErrorMessage = (response?: Response): string => { | ||
const fallbackMsg = 'an unknown error'; | ||
|
||
if (!response) { | ||
return fallbackMsg; | ||
} | ||
|
||
const code = typeof response.status === 'number' ? response.status : ''; | ||
const status = `${code} ${response.statusText || ''}`.trim(); | ||
return status ? `status code ${status}` : fallbackMsg; | ||
}; | ||
|
||
interface ErrorOptions { | ||
/** | ||
* Expect cause to be an Error instance | ||
*/ | ||
cause: Error; | ||
} | ||
|
||
/** | ||
* HttpError provides a single error class to handle all failure responses | ||
* from the fetch request. | ||
*/ | ||
class HttpError extends Error { | ||
public status: number | undefined; | ||
|
||
constructor(response?: Response, options?: ErrorOptions) { | ||
let errorMsg = 'an unknown error'; | ||
|
||
if (options?.cause.name === 'SyntaxError') { | ||
errorMsg = 'an invalid json response'; | ||
} else { | ||
errorMsg = getErrorMessage(response); | ||
} | ||
|
||
super(`Request failed with ${errorMsg}`, options); | ||
|
||
this.name = 'HttpError'; | ||
this.status = response?.status; | ||
} | ||
} | ||
|
||
export default HttpError; |
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 +1,2 @@ | ||
export { API_ROOT, fetch, getJson, type HttpError } from './api'; | ||
export { API_ROOT, fetch, getJson } from './api'; | ||
export { default as HttpError } from './HttpError'; |
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