-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some failure codes when adding a server
Partly addresses #925.
- Loading branch information
Showing
6 changed files
with
153 additions
and
36 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* Create an `Error` with a `code` property. | ||
* | ||
* @example | ||
* const err = new ErrorWithCode('INVALID_DATA', 'data was invalid') | ||
* err.message | ||
* // => 'data was invalid' | ||
* err.code | ||
* // => 'INVALID_DATA' | ||
*/ | ||
export class ErrorWithCode extends Error { | ||
/** | ||
* @param {string} code | ||
* @param {string} message | ||
* @param {object} [options] | ||
* @param {unknown} [options.cause] | ||
*/ | ||
constructor(code, message, options) { | ||
super(message, options) | ||
/** @readonly */ this.code = code | ||
} | ||
} | ||
|
||
/** | ||
* Get the error message from an object if possible. | ||
* Otherwise, stringify the argument. | ||
* | ||
* @param {unknown} maybeError | ||
* @returns {string} | ||
* @example | ||
* try { | ||
* // do something | ||
* } catch (err) { | ||
* console.error(getErrorMessage(err)) | ||
* } | ||
*/ | ||
export function getErrorMessage(maybeError) { | ||
if (maybeError && typeof maybeError === 'object' && 'message' in maybeError) { | ||
try { | ||
const { message } = maybeError | ||
if (typeof message === 'string') return message | ||
} catch (_err) { | ||
// Ignored | ||
} | ||
} | ||
return 'unknown error' | ||
} |
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import assert from 'node:assert/strict' | ||
import test, { describe } from 'node:test' | ||
import { ErrorWithCode, getErrorMessage } from '../../src/lib/error.js' | ||
|
||
describe('ErrorWithCode', () => { | ||
test('ErrorWithCode with two arguments', () => { | ||
const err = new ErrorWithCode('MY_CODE', 'my message') | ||
assert.equal(err.code, 'MY_CODE') | ||
assert.equal(err.message, 'my message') | ||
assert(err instanceof Error) | ||
}) | ||
|
||
test('ErrorWithCode with three arguments', () => { | ||
const otherError = new Error('hello') | ||
const err = new ErrorWithCode('MY_CODE', 'my message', { | ||
cause: otherError, | ||
}) | ||
assert.equal(err.code, 'MY_CODE') | ||
assert.equal(err.message, 'my message') | ||
assert.equal(err.cause, otherError) | ||
assert(err instanceof Error) | ||
}) | ||
}) | ||
|
||
describe('getErrorMessage', () => { | ||
test('from objects without a string message', () => { | ||
const testCases = [ | ||
undefined, | ||
null, | ||
['ignored'], | ||
{ message: 123 }, | ||
{ | ||
get message() { | ||
throw new Error('this should not crash') | ||
}, | ||
}, | ||
] | ||
|
||
for (const testCase of testCases) { | ||
assert.equal(getErrorMessage(testCase), 'unknown error') | ||
} | ||
}) | ||
|
||
test('from objects with a string message', () => { | ||
class WithInheritedMessage { | ||
get message() { | ||
return 'foo' | ||
} | ||
} | ||
|
||
const testCases = [ | ||
{ message: 'foo' }, | ||
new Error('foo'), | ||
{ | ||
get message() { | ||
return 'foo' | ||
}, | ||
}, | ||
new WithInheritedMessage(), | ||
] | ||
|
||
for (const testCase of testCases) { | ||
assert.equal(getErrorMessage(testCase), 'foo') | ||
} | ||
}) | ||
}) |