├── .airtap.yml ├── .github ├── dependabot.yml └── workflows │ ├── release.yml │ └── test.yml ├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── UPGRADING.md ├── errors.js ├── package.json └── test.js /.airtap.yml: -------------------------------------------------------------------------------- 1 | providers: 2 | - airtap-playwright 3 | 4 | browsers: 5 | - name: chromium 6 | - name: firefox 7 | - name: webkit -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: npm 4 | directory: / 5 | schedule: 6 | interval: monthly 7 | ignore: 8 | - dependency-name: dependency-check 9 | - package-ecosystem: github-actions 10 | directory: / 11 | schedule: 12 | interval: monthly 13 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | on: 3 | push: 4 | tags: ['*'] 5 | permissions: 6 | contents: write 7 | jobs: 8 | release: 9 | name: Release 10 | runs-on: ubuntu-latest 11 | steps: 12 | - name: Checkout 13 | uses: actions/checkout@v4 14 | - name: Create GitHub release 15 | uses: docker://antonyurchenko/git-release:v4 16 | env: 17 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 18 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | on: [push, pull_request] 3 | jobs: 4 | test: 5 | runs-on: ubuntu-latest 6 | strategy: 7 | matrix: 8 | node: [10, 12, 14] 9 | name: Node ${{ matrix.node }} 10 | steps: 11 | - name: Checkout 12 | uses: actions/checkout@v4 13 | - name: Use node ${{ matrix.node }} 14 | uses: actions/setup-node@v3 15 | with: 16 | node-version: ${{ matrix.node }} 17 | - name: Install 18 | run: npm install 19 | - name: Test 20 | run: npm test 21 | - name: Coverage 22 | run: npm run coverage 23 | - name: Codecov 24 | uses: codecov/codecov-action@v3 25 | with: 26 | file: coverage/lcov.info 27 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | .nyc_output/ 3 | coverage 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## [3.0.1] - 2021-09-24 4 | 5 | ### Fixed 6 | 7 | - Don't wrap existing errors ([#37](https://github.com/Level/errors/issues/37)) ([`189f2b1`](https://github.com/Level/errors/commit/189f2b1)) (Vincent Weevers) 8 | 9 | ## [3.0.0] - 2021-04-09 10 | 11 | _If you are upgrading: please see [`UPGRADING.md`](UPGRADING.md)._ 12 | 13 | ### Changed 14 | 15 | - **Breaking:** drop node 6 and 8 ([Level/community#98](https://github.com/Level/community/issues/98)) ([`bb296b9`](https://github.com/Level/errors/commit/bb296b9)) (Vincent Weevers) 16 | - **Breaking:** modernize syntax and bump `standard` ([Level/community#98](https://github.com/Level/community/issues/98)) ([`5a6de73`](https://github.com/Level/errors/commit/5a6de73)) (Vincent Weevers) 17 | - Bump `errno` from 0.1.8 to 1.0.0 ([#34](https://github.com/Level/errors/issues/34)) ([`bb5ec3a`](https://github.com/Level/errors/commit/bb5ec3a), [`6756d89`](https://github.com/Level/errors/commit/6756d89)) (Lars-Magnus Skog, Vincent Weevers) 18 | - Add `files` to `package.json` ([`83d2dcd`](https://github.com/Level/errors/commit/83d2dcd)) (Vincent Weevers). 19 | 20 | ## [2.0.1] - 2019-04-05 21 | 22 | ### Changed 23 | 24 | - Upgrade `standard` devDependency from `^11.0.0` to `^12.0.0` ([#19](https://github.com/Level/errors/issues/19)) ([**@ralphtheninja**](https://github.com/ralphtheninja)) 25 | - Apply common project tweaks ([#20](https://github.com/Level/errors/issues/20), [#21](https://github.com/Level/errors/issues/21)) ([**@vweevers**](https://github.com/vweevers)) 26 | - Tweak license ([`b61e01b`](https://github.com/Level/errors/commit/b61e01b)) ([**@ralphtheninja**](https://github.com/ralphtheninja)) 27 | - Tweak copyright years for less maintenance ([`772e911`](https://github.com/Level/errors/commit/772e911)) ([**@ralphtheninja**](https://github.com/ralphtheninja)) 28 | 29 | ### Added 30 | 31 | - Add `nyc` and `coveralls` ([#18](https://github.com/Level/errors/issues/18), [#22](https://github.com/Level/errors/issues/22)) ([**@ralphtheninja**](https://github.com/ralphtheninja), [**@vweevers**](https://github.com/vweevers)) 32 | 33 | ### Removed 34 | 35 | - Remove node 9 from travis ([`9254882`](https://github.com/Level/errors/commit/9254882)) ([**@ralphtheninja**](https://github.com/ralphtheninja)) 36 | - Remove experimental typings ([`0ad3b37`](https://github.com/Level/errors/commit/0ad3b37)) ([**@vweevers**](https://github.com/vweevers)) 37 | - Remove contributors from `package.json` ([`29e1ae1`](https://github.com/Level/errors/commit/29e1ae1)) ([**@ralphtheninja**](https://github.com/ralphtheninja)) 38 | - Remove copyright headers from code ([`f5e5e40`](https://github.com/Level/errors/commit/f5e5e40)) ([**@ralphtheninja**](https://github.com/ralphtheninja)) 39 | 40 | ## [2.0.0] - 2018-05-13 41 | 42 | _If you are upgrading: please see [`UPGRADING.md`](UPGRADING.md)._ 43 | 44 | ### Changed 45 | 46 | - Tweak readme, add npm and node badges ([`f80d1b7`](https://github.com/Level/errors/commit/f80d1b7)) ([**@ralphtheninja**](https://github.com/ralphtheninja)) 47 | - Upgrade `standard` devDependency from `^10.0.3` to `^11.0.0` ([#12](https://github.com/Level/errors/issues/12)) ([**@ralphtheninja**](https://github.com/ralphtheninja)) 48 | 49 | ### Added 50 | 51 | - Add node 10 ([`b002756`](https://github.com/Level/errors/commit/b002756)) ([**@ralphtheninja**](https://github.com/ralphtheninja)) 52 | 53 | ### Removed 54 | 55 | - Drop node 0.12, 4, 5 and 7 ([`b002756`](https://github.com/Level/errors/commit/b002756), [`150a3c6`](https://github.com/Level/errors/commit/150a3c6)) ([**@ralphtheninja**](https://github.com/ralphtheninja)) 56 | 57 | ## [1.1.2] - 2017-11-13 58 | 59 | ### Changed 60 | 61 | - Update README style ([#11](https://github.com/Level/errors/issues/11)) ([**@ralphtheninja**](https://github.com/ralphtheninja)) 62 | 63 | ### Added 64 | 65 | - Add `standard` devDependency ([#10](https://github.com/Level/errors/issues/10)) ([**@ralphtheninja**](https://github.com/ralphtheninja)) 66 | 67 | ## [1.1.1] - 2017-09-10 68 | 69 | ### Fixed 70 | 71 | - Fix error `cause` argument in typings ([#9](https://github.com/Level/errors/issues/9)) ([**@MeirionHughes**](https://github.com/MeirionHughes)) 72 | 73 | ## [1.1.0] - 2017-09-10 74 | 75 | ### Changed 76 | 77 | - Tweak README ([`64b4842`](https://github.com/Level/errors/commit/64b4842)) ([**@ralphtheninja**](https://github.com/ralphtheninja)) 78 | 79 | ### Added 80 | 81 | - Add basic typings ([#8](https://github.com/Level/errors/issues/8)) ([**@MeirionHughes**](https://github.com/MeirionHughes)) 82 | 83 | ## [1.0.5] - 2017-08-15 84 | 85 | ### Changed 86 | 87 | - Update copyright year from 2015 to 2017 ([#7](https://github.com/Level/errors/issues/7)) ([**@ralphtheninja**](https://github.com/ralphtheninja)) 88 | 89 | ### Added 90 | 91 | - Add node 4, 5, 6, 7 and 8 ([#7](https://github.com/Level/errors/issues/7)) ([**@ralphtheninja**](https://github.com/ralphtheninja)) 92 | - Add Greenkeeper ([#3](https://github.com/Level/errors/issues/3), [#6](https://github.com/Level/errors/issues/6)) ([**@ralphtheninja**](https://github.com/ralphtheninja)) 93 | 94 | ### Removed 95 | 96 | - Drop node 0.10 and iojs ([#7](https://github.com/Level/errors/issues/7)) ([**@ralphtheninja**](https://github.com/ralphtheninja)) 97 | 98 | ## [1.0.4] - 2015-08-25 99 | 100 | ### Added 101 | 102 | - Document API ([#2](https://github.com/Level/errors/issues/2)) ([**@juliangruber**](https://github.com/juliangruber)) 103 | 104 | ## [1.0.3] - 2015-03-21 105 | 106 | ### Fixed 107 | 108 | - Add node 0.10 to travis ([`fcc758c`](https://github.com/Level/errors/commit/fcc758c)) ([**@ralphtheninja**](https://github.com/ralphtheninja)) 109 | 110 | ## [1.0.2] - 2015-03-21 111 | 112 | ### Changed 113 | 114 | - Tweak README to have same style as `codec` ([`d5e0cba`](https://github.com/Level/errors/commit/d5e0cba)) ([**@ralphtheninja**](https://github.com/ralphtheninja)) 115 | 116 | ### Added 117 | 118 | - Add travis ([`5f6d1cd`](https://github.com/Level/errors/commit/5f6d1cd), [`58690fa`](https://github.com/Level/errors/commit/58690fa), [`36c3199`](https://github.com/Level/errors/commit/36c3199)) ([**@ralphtheninja**](https://github.com/ralphtheninja)) 119 | 120 | ## [1.0.1] - 2015-03-20 121 | 122 | :seedling: Initial release. 123 | 124 | [3.0.1]: https://github.com/Level/errors/compare/v3.0.0...v3.0.1 125 | 126 | [3.0.0]: https://github.com/Level/errors/compare/v2.0.1...v3.0.0 127 | 128 | [2.0.1]: https://github.com/Level/errors/compare/v2.0.0...v2.0.1 129 | 130 | [2.0.0]: https://github.com/Level/errors/compare/v1.1.2...v2.0.0 131 | 132 | [1.1.2]: https://github.com/Level/errors/compare/v1.1.1...v1.1.2 133 | 134 | [1.1.1]: https://github.com/Level/errors/compare/v1.1.0...v1.1.1 135 | 136 | [1.1.0]: https://github.com/Level/errors/compare/v1.0.5...v1.1.0 137 | 138 | [1.0.5]: https://github.com/Level/errors/compare/v1.0.4...v1.0.5 139 | 140 | [1.0.4]: https://github.com/Level/errors/compare/v1.0.3...v1.0.4 141 | 142 | [1.0.3]: https://github.com/Level/errors/compare/v1.0.2...v1.0.3 143 | 144 | [1.0.2]: https://github.com/Level/errors/compare/v1.0.1...v1.0.2 145 | 146 | [1.0.1]: https://github.com/Level/errors/releases/tag/v1.0.1 147 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright © 2012 The contributors to level-errors. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # level-errors 2 | 3 | **Superseded by [`abstract-level`](https://github.com/Level/abstract-level). Please see [Frequently Asked Questions](https://github.com/Level/community#faq).** 4 | 5 | ## API 6 | 7 | **If you are upgrading:** please see [`UPGRADING.md`](UPGRADING.md). 8 | 9 | ### `.LevelUPError()` 10 | 11 | Generic error base class. 12 | 13 | ### `.InitializationError()` 14 | 15 | Error initializing the database, like when the database's location argument is missing. 16 | 17 | ### `.OpenError()` 18 | 19 | Error opening the database. 20 | 21 | ### `.ReadError()` 22 | 23 | Error reading from the database. 24 | 25 | ### `.WriteError()` 26 | 27 | Error writing to the database. 28 | 29 | ### `.NotFoundError()` 30 | 31 | Data not found error. 32 | 33 | Has extra properties: 34 | 35 | - `notFound`: `true` 36 | - `status`: 404 37 | 38 | ### `.EncodingError()` 39 | 40 | Error encoding data. 41 | 42 | ## Contributing 43 | 44 | [`Level/errors`](https://github.com/Level/errors) is an **OPEN Open Source Project**. This means that: 45 | 46 | > Individuals making significant and valuable contributions are given commit-access to the project to contribute as they see fit. This project is more like an open wiki than a standard guarded open source project. 47 | 48 | See the [Contribution Guide](https://github.com/Level/community/blob/master/CONTRIBUTING.md) for more details. 49 | 50 | ## License 51 | 52 | [MIT](LICENSE) 53 | 54 | [levelup]: https://github.com/Level/levelup 55 | -------------------------------------------------------------------------------- /UPGRADING.md: -------------------------------------------------------------------------------- 1 | # Upgrade Guide 2 | 3 | This document describes breaking changes and how to upgrade. For a complete list of changes including minor and patch releases, please refer to the [changelog](CHANGELOG.md). 4 | 5 | ## 3.0.0 6 | 7 | This release drops support of legacy runtime environments ([Level/community#98](https://github.com/Level/community/issues/98)): 8 | 9 | - Node.js 6 and 8 10 | - Internet Explorer 11 11 | - Safari 9-11 12 | - Stock Android browser (AOSP). 13 | 14 | ## 2.0.0 15 | 16 | Dropped node 0.12, 4, 5 and 7. 17 | -------------------------------------------------------------------------------- /errors.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | function createError (type, Proto) { 4 | const Err = function (message, cause) { 5 | if (typeof message === 'object' && message !== null) { 6 | // Can be passed just a cause 7 | cause = cause || message 8 | message = message.message || message.name 9 | } 10 | 11 | message = message || '' 12 | cause = cause || undefined 13 | 14 | // If input is already of type, return as-is to keep its stack trace. 15 | // Avoid instanceof, for when node_modules has multiple copies of level-errors. 16 | if (typeof cause === 'object' && cause.type === type && cause.message === message) { 17 | return cause 18 | } 19 | 20 | Object.defineProperty(this, 'type', { value: type, enumerable: false, writable: true, configurable: true }) 21 | Object.defineProperty(this, 'name', { value: type, enumerable: false, writable: true, configurable: true }) 22 | Object.defineProperty(this, 'cause', { value: cause, enumerable: false, writable: true, configurable: true }) 23 | Object.defineProperty(this, 'message', { value: message, enumerable: false, writable: true, configurable: true }) 24 | 25 | Error.call(this) 26 | 27 | if (typeof Error.captureStackTrace === 'function') { 28 | Error.captureStackTrace(this, Err) 29 | } 30 | } 31 | 32 | Err.prototype = new Proto() 33 | return Err 34 | } 35 | 36 | const LevelUPError = createError('LevelUPError', Error) 37 | 38 | module.exports = { 39 | LevelUPError: LevelUPError, 40 | InitializationError: createError('InitializationError', LevelUPError), 41 | OpenError: createError('OpenError', LevelUPError), 42 | ReadError: createError('ReadError', LevelUPError), 43 | WriteError: createError('WriteError', LevelUPError), 44 | NotFoundError: createError('NotFoundError', LevelUPError), 45 | EncodingError: createError('EncodingError', LevelUPError) 46 | } 47 | 48 | module.exports.NotFoundError.prototype.notFound = true 49 | module.exports.NotFoundError.prototype.status = 404 50 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "level-errors", 3 | "version": "3.0.1", 4 | "description": "Error types for levelup", 5 | "license": "MIT", 6 | "main": "errors.js", 7 | "scripts": { 8 | "test": "standard && hallmark && nyc node test.js", 9 | "test-browsers-local": "airtap --coverage test.js", 10 | "coverage": "nyc report -r lcovonly", 11 | "hallmark": "hallmark --fix", 12 | "dependency-check": "dependency-check . test.js", 13 | "prepublishOnly": "npm run dependency-check" 14 | }, 15 | "files": [ 16 | "errors.js", 17 | "CHANGELOG.md", 18 | "UPGRADING.md" 19 | ], 20 | "devDependencies": { 21 | "airtap": "^4.0.3", 22 | "airtap-playwright": "^1.0.1", 23 | "dependency-check": "^3.3.0", 24 | "hallmark": "^4.0.0", 25 | "nyc": "^15.1.0", 26 | "standard": "^17.0.0", 27 | "tape": "^5.0.1" 28 | }, 29 | "repository": { 30 | "type": "git", 31 | "url": "https://github.com/Level/errors.git" 32 | }, 33 | "homepage": "https://github.com/Level/errors", 34 | "keywords": [ 35 | "level", 36 | "leveldb", 37 | "levelup", 38 | "leveldown", 39 | "errors" 40 | ], 41 | "engines": { 42 | "node": ">=10" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const test = require('tape') 4 | const errors = require('.') 5 | 6 | test('all errors are instances of Error and LevelUPError', function (t) { 7 | const LevelUPError = errors.LevelUPError 8 | const keys = Object.keys(errors) 9 | 10 | keys.forEach(function (key) { 11 | t.ok(new errors[key]() instanceof Error) 12 | t.ok(new errors[key]() instanceof LevelUPError) 13 | }) 14 | 15 | t.end() 16 | }) 17 | 18 | test('error with message has expected properties', function (t) { 19 | const error = new errors.ReadError('foo') 20 | 21 | t.is(error.type, 'ReadError') 22 | t.is(error.name, 'ReadError') 23 | t.is(error.cause, undefined) 24 | t.is(error.message, 'foo') 25 | t.end() 26 | }) 27 | 28 | test('error without message has expected properties', function (t) { 29 | const error = new errors.ReadError() 30 | 31 | t.is(error.type, 'ReadError') 32 | t.is(error.name, 'ReadError') 33 | t.is(error.cause, undefined) 34 | t.is(error.message, '') 35 | t.end() 36 | }) 37 | 38 | test('error with cause has expected properties', function (t) { 39 | const cause = new Error('foo') 40 | const error = new errors.ReadError(cause) 41 | 42 | t.is(error.type, 'ReadError') 43 | t.is(error.name, 'ReadError') 44 | t.is(error.cause, cause) 45 | t.is(error.message, 'foo') 46 | t.end() 47 | }) 48 | 49 | test('error with message and cause has expected properties', function (t) { 50 | const cause = new Error('foo') 51 | const error = new errors.ReadError('bar', cause) 52 | 53 | t.is(error.type, 'ReadError') 54 | t.is(error.name, 'ReadError') 55 | t.is(error.cause, cause) 56 | t.is(error.message, 'bar') 57 | t.end() 58 | }) 59 | 60 | test('NotFoundError has special properties', function (t) { 61 | const error = new errors.NotFoundError() 62 | t.is(error.notFound, true) 63 | t.is(error.status, 404) 64 | t.end() 65 | }) 66 | 67 | test('error message is writable to mirror node core conventions', function (t) { 68 | const error = new errors.WriteError('foo') 69 | error.message = 'Got error: ' + error.message 70 | t.is(error.message, 'Got error: foo') 71 | t.end() 72 | }) 73 | 74 | test('returns original instance if cause is the same type', function (t) { 75 | const cause = new errors.NotFoundError('Key not found in database [foo]') 76 | const error = new errors.NotFoundError(cause) 77 | t.is(cause, error, 'same instance') 78 | t.is(error.message, 'Key not found in database [foo]') 79 | t.end() 80 | }) 81 | 82 | test('returns new instance if cause prototype is different', function (t) { 83 | const cause = new errors.NotFoundError('Key not found in database [foo]') 84 | const error = new errors.WriteError(cause) 85 | t.isNot(cause, error, 'new instance') 86 | t.is(error.message, 'Key not found in database [foo]') 87 | t.end() 88 | }) 89 | 90 | test('returns original instance if message and cause are the same', function (t) { 91 | const cause = new errors.NotFoundError('Key not found in database [foo]') 92 | const error = new errors.NotFoundError('Key not found in database [foo]', cause) 93 | t.is(cause, error, 'same instance') 94 | t.end() 95 | }) 96 | 97 | test('returns new instance if message is different', function (t) { 98 | const cause = new errors.NotFoundError('Key not found in database [foo]') 99 | const error = new errors.NotFoundError('Key not found in database [bar]', cause) 100 | t.isNot(cause, error, 'new instance') 101 | t.end() 102 | }) 103 | --------------------------------------------------------------------------------