This repository was archived by the owner on Dec 20, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Don't wrap existing errors #37
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
eeab1b5
Add tests for error properties
vweevers 3576ead
Inline `errno` package
vweevers cb62efa
Fix `message` and `cause` bugs
vweevers 6db33c7
Make error properties configurable and writable
vweevers 8c2de6b
Don't wrap existing errors
vweevers 4b77e8f
Fix Error prototype
vweevers fa4c505
Use `t.is()`
vweevers 24c2c77
Tweak test name to clarify `writable` choice
vweevers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1,18 +1,49 @@ | ||
'use strict' | ||
|
||
const createError = require('errno').create | ||
const LevelUPError = createError('LevelUPError') | ||
const NotFoundError = createError('NotFoundError', LevelUPError) | ||
function createError (type, Proto) { | ||
const Err = function (message, cause) { | ||
if (typeof message === 'object' && message !== null) { | ||
// Can be passed just a cause | ||
cause = cause || message | ||
message = message.message || message.name | ||
} | ||
|
||
NotFoundError.prototype.notFound = true | ||
NotFoundError.prototype.status = 404 | ||
message = message || '' | ||
cause = cause || undefined | ||
juliangruber marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// If input is already of type, return as-is to keep its stack trace. | ||
// Avoid instanceof, for when node_modules has multiple copies of level-errors. | ||
if (typeof cause === 'object' && cause.type === type && cause.message === message) { | ||
return cause | ||
} | ||
|
||
Object.defineProperty(this, 'type', { value: type, enumerable: false, writable: true, configurable: true }) | ||
Object.defineProperty(this, 'name', { value: type, enumerable: false, writable: true, configurable: true }) | ||
Object.defineProperty(this, 'cause', { value: cause, enumerable: false, writable: true, configurable: true }) | ||
Object.defineProperty(this, 'message', { value: message, enumerable: false, writable: true, configurable: true }) | ||
|
||
Error.call(this) | ||
|
||
if (typeof Error.captureStackTrace === 'function') { | ||
Error.captureStackTrace(this, Err) | ||
} | ||
} | ||
|
||
Err.prototype = new Proto() | ||
return Err | ||
} | ||
|
||
const LevelUPError = createError('LevelUPError', Error) | ||
|
||
module.exports = { | ||
LevelUPError: LevelUPError, | ||
InitializationError: createError('InitializationError', LevelUPError), | ||
OpenError: createError('OpenError', LevelUPError), | ||
ReadError: createError('ReadError', LevelUPError), | ||
WriteError: createError('WriteError', LevelUPError), | ||
NotFoundError: NotFoundError, | ||
NotFoundError: createError('NotFoundError', LevelUPError), | ||
EncodingError: createError('EncodingError', LevelUPError) | ||
} | ||
|
||
module.exports.NotFoundError.prototype.notFound = true | ||
module.exports.NotFoundError.prototype.status = 404 |
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.