This repository has been 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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
So that when `abstract-leveldown` starts using `level-errors`, and such a db is wrapped with `levelup`, `levelup` will not rewrap errors and lose stack trace information in the process. I.e.: ``` // Error created by abstract-leveldown const err = new ReadError('example') // Error created by levelup const wrapped = new ReadError(err) assert(wrapped === err) ``` Ref Level/community#58
- Loading branch information
Showing
3 changed files
with
121 additions
and
12 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
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 | ||
|
||
// 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 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