-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Improve errors Renamed `TransloaditError` to `ApiError`. Differences between `TransloaditError` and `ApiError`: - Moved `TransloaditError.response.body` to `ApiError.response` - Removed `TransloaditError.assemblyId` (can now be found in `ApiError.response.assembly_id` - Removed `TransloaditError.transloaditErrorCode` (can now be found in `ApiError.response.error` - `ApiError` does not inherit from `got.HTTPError`, but `ApiError.cause` will be the `got.HTTPError` instance that caused this error (except for when Tranloadit API responds with HTTP 200 and `error` prop set in JSON response, in which case cause will be `undefined`). Note that (just like before) when the Transloadit API responds with an error we will always throw a `ApiError` - In all other cases (like request timeout, connection error, TypeError etc.), we don't wrap the error in `ApiError`. Also improved error stack traces, added a unit test in `mock-http.test.ts` that verifies the stack trace. * Improve errors alternative implementation (#212) * make alternative implementation ...of #211 where `ApiError` has all the API response properties directly on it instaed of inside a `response` object - `ApiError.response.error` -> `ApiError.code` - `ApiError.response.message` -> `ApiError.rawMessage` - `ApiError.response.assembly_id` -> `ApiError.assemblyId` - `ApiError.response.assembly_ssl_url` -> `ApiError.assemblySslUrl` * fix formatting * Update README.md Co-authored-by: Remco Haszing <[email protected]> * remove stack hack #211 (comment) * improve assertion * fix typo * fix formatting --------- Co-authored-by: Remco Haszing <[email protected]> --------- Co-authored-by: Remco Haszing <[email protected]>
- Loading branch information
1 parent
98a6419
commit 94356cb
Showing
7 changed files
with
165 additions
and
188 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { HTTPError } from 'got' | ||
|
||
export interface TransloaditErrorResponseBody { | ||
error?: string | ||
message?: string | ||
assembly_ssl_url?: string | ||
assembly_id?: string | ||
} | ||
|
||
export class ApiError extends Error { | ||
override name = 'ApiError' | ||
|
||
// there might not be an error code (or message) if the server didn't respond with any JSON response at all | ||
// e.g. if there was a 500 in the HTTP reverse proxy | ||
code?: string | ||
rawMessage?: string | ||
assemblySslUrl?: string | ||
assemblyId?: string | ||
|
||
override cause?: HTTPError | undefined | ||
|
||
constructor(params: { cause?: HTTPError; body: TransloaditErrorResponseBody | undefined }) { | ||
const { cause, body = {} } = params | ||
|
||
const parts = ['API error'] | ||
if (cause?.response.statusCode) parts.push(`(HTTP ${cause.response.statusCode})`) | ||
if (body.error) parts.push(`${body.error}:`) | ||
if (body.message) parts.push(body.message) | ||
if (body.assembly_ssl_url) parts.push(body.assembly_ssl_url) | ||
|
||
const message = parts.join(' ') | ||
|
||
super(message) | ||
this.rawMessage = body.message | ||
this.assemblyId = body.assembly_id | ||
this.assemblySslUrl = body.assembly_ssl_url | ||
this.code = body.error | ||
this.cause = cause | ||
} | ||
} |
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 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
Oops, something went wrong.