Skip to content

Commit

Permalink
Define custom error types (#186)
Browse files Browse the repository at this point in the history
We currently decorate plain Error objects. TypeScript doesn’t like this.
We better use custom error types.
  • Loading branch information
remcohaszing authored Sep 2, 2024
1 parent f4831a2 commit a5e4b98
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
5 changes: 1 addition & 4 deletions src/InconsistentResponseError.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
class InconsistentResponseError extends Error {
constructor(message) {
super(message)
this.name = 'InconsistentResponseError'
}
name = 'InconsistentResponseError'
}

module.exports = InconsistentResponseError
7 changes: 7 additions & 0 deletions src/PollingTimeoutError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class PollingTimeoutError extends Error {
name = 'PollingTimeoutError'

code = 'POLLING_TIMED_OUT'
}

module.exports = PollingTimeoutError
13 changes: 4 additions & 9 deletions src/Transloadit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const pMap = require('p-map')

const InconsistentResponseError = require('./InconsistentResponseError')
const PaginationStream = require('./PaginationStream')
const PollingTimeoutError = require('./PollingTimeoutError')
const TransloaditError = require('./TransloaditError')
const pkg = require('../package.json')
const tus = require('./tus')

Expand Down Expand Up @@ -60,12 +62,7 @@ function checkResult(result) {
// In case server returned a successful HTTP status code, but an `error` in the JSON object
// This happens sometimes when createAssembly with an invalid file (IMPORT_FILE_ERROR)
if (typeof result === 'object' && result !== null && typeof result.error === 'string') {
const err = new Error('Error in response')
// Mimic got HTTPError structure
err.response = {
body: result,
}
throw decorateHttpError(err, result)
throw decorateHttpError(new TransloaditError('Error in response', result), result)
}
}

Expand Down Expand Up @@ -285,9 +282,7 @@ class TransloaditClient {

const nowMs = getHrTimeMs()
if (timeout != null && nowMs - startTimeMs >= timeout) {
const err = new Error('Polling timed out')
err.code = 'POLLING_TIMED_OUT'
throw err
throw new PollingTimeoutError('Polling timed out')
}
await new Promise((resolve) => setTimeout(resolve, interval))
}
Expand Down
10 changes: 10 additions & 0 deletions src/TransloaditError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class TransloaditError extends Error {
name = 'TransloaditError'

constructor(message, body) {
super(message)
this.response = { body }
}
}

module.exports = TransloaditError

0 comments on commit a5e4b98

Please sign in to comment.