-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): throw on error and export types in JavaScript SDK (#2180)
- Loading branch information
1 parent
a03739a
commit 5a5f911
Showing
20 changed files
with
104 additions
and
88 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import type { UnexpectedProblemResponse } from './schemas.js' | ||
|
||
/** | ||
* Request options | ||
*/ | ||
export type RequestOptions = Pick<RequestInit, 'signal'> | ||
|
||
/** | ||
* An error that occurred during an HTTP request | ||
*/ | ||
export class HTTPError extends Error { | ||
name = 'HTTPError' | ||
|
||
constructor( | ||
public message: string, | ||
public type: string, | ||
public title: string, | ||
public status: number, | ||
protected __raw?: Record<string, any> | ||
) { | ||
super(message) | ||
} | ||
|
||
static fromResponse(resp: { | ||
response: Response | ||
error?: UnexpectedProblemResponse | ||
}): HTTPError { | ||
if ( | ||
resp.response.headers.get('Content-Type') === | ||
'application/problem+json' && | ||
resp.error | ||
) { | ||
return new HTTPError( | ||
resp.error.detail, | ||
resp.error.type, | ||
resp.error.title, | ||
resp.error.status ?? resp.response.status, | ||
resp.error | ||
) | ||
} | ||
|
||
return new HTTPError( | ||
`Request failed: ${resp.response.statusText}`, | ||
resp.response.statusText, | ||
resp.response.statusText, | ||
resp.response.status | ||
) | ||
} | ||
|
||
getField(key: string) { | ||
return this.__raw?.[key] | ||
} | ||
} | ||
|
||
/** | ||
* Check if an error is an HTTPError | ||
* @param error - The error to check | ||
* @returns Whether the error is an HTTPError | ||
*/ | ||
export function isHTTPError(error: unknown): error is HTTPError { | ||
return error instanceof 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
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
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
File renamed without changes.
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