-
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.
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.
- Loading branch information
Showing
7 changed files
with
190 additions
and
174 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,50 @@ | ||
import { HTTPError } from 'got' | ||
|
||
export interface TransloaditErrorResponseBody { | ||
error?: string | ||
message?: string | ||
http_code?: string | ||
assembly_ssl_url?: string | ||
assembly_id?: string | ||
} | ||
|
||
export class ApiError extends Error { | ||
override name = 'ApiError' | ||
|
||
response: TransloaditErrorResponseBody | ||
|
||
override cause?: HTTPError | undefined | ||
|
||
constructor(params: { | ||
cause?: HTTPError | ||
appendStack?: string | ||
body: TransloaditErrorResponseBody | undefined | ||
}) { | ||
const { cause, body, appendStack } = 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) | ||
|
||
// if we have a cause, use the stack trace from it instead | ||
if (cause != null && typeof cause.stack === 'string') { | ||
const indexOfMessageEnd = cause.stack.indexOf(cause.message) + cause.message.length | ||
const gotStacktrace = cause.stack.slice(indexOfMessageEnd) | ||
this.stack = `${message}${gotStacktrace}` | ||
} | ||
|
||
// If we have an original stack, append it to the bottom, because `got`s stack traces are not very good | ||
if (this.stack != null && appendStack != null) { | ||
this.stack += `\n${appendStack.replace(/^([^\n]+\n)/, '')}` | ||
} | ||
|
||
this.response = body ?? {} | ||
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.