Tidy up for release

This commit is contained in:
John Lees-Miller 2018-04-21 23:24:23 +01:00
parent a94b34f37f
commit 138b339c0f
5 changed files with 207 additions and 53 deletions

21
libraries/o-error/LICENSE Normal file
View file

@ -0,0 +1,21 @@
The MIT License
Copyright (c) 2016-2018 Overleaf https://www.overleaf.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View file

@ -0,0 +1,76 @@
# overleaf-error-type
Make custom error types that pass `instanceof` checks, have stack traces and support custom messages and properties.
The approach is based mainly on https://gist.github.com/justmoon/15511f92e5216fa2624b; it just tries to DRY it up a bit.
## Usage
### Define a standalone error class
```js
const errorType = require('overleaf-error-type')
const CustomError = errorType.define('CustomError')
function doSomethingBad () {
throw new CustomError()
}
doSomethingBad()
// =>
// CustomError <-- correct name
// at doSomethingBad (repl:2:9) <-- stack trace
```
### Define an error subclass
```js
const SubCustomError = errorType.extend(CustomError, 'SubCustomError')
try {
throw new SubCustomError()
} catch (err) {
console.log(err.name) // => SubCustomError
console.log(err instanceof SubCustomError) // => true
console.log(err instanceof CustomError) // => true
console.log(err instanceof Error) // => true
}
```
### Add custom message and/or properties
```js
const UserNotFoundError = errorType.define('UserNotFoundError',
function (userId) {
this.message = `User not found: ${userId}`
this.userId = userId
})
throw new UserNotFoundError(123)
// => UserNotFoundError: User not found: 123
```
### Add custom Error types under an existing class
```js
class User {
static lookup (userId) {
throw new User.UserNotFoundError(userId)
}
}
errorType.defineIn(User, 'UserNotFoundError', function (userId) {
this.message = `User not found: ${userId}`
this.userId = userId
})
User.lookup(123)
// =>
// UserNotFoundError: User not found: 123
// at Function.lookup (repl:3:11)
```
## References
- https://gist.github.com/justmoon/15511f92e5216fa2624b
- [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)

View file

@ -3,15 +3,8 @@
var util = require('util')
/**
* Make custom error types. There are many, many modules for this, but they all
* seem a bit weird. This approach is based on
* https://gist.github.com/justmoon/15511f92e5216fa2624b
* which seems sensible. This module mainly tries to DRY it up a bit. It also
* incorporates some ideas from the verror package. Maybe it can become its own
* package one day.
*
* TODO: Will this work under browserify?
*
* Make custom error types that pass `instanceof` checks, have stack traces and
* support custom messages and properties.
* @module
*/

View file

@ -1,97 +1,147 @@
{
"name": "overleaf-error-type",
"name": "error-type",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"chai": {
"version": "3.4.1",
"from": "chai@>=3.3.0 <4.0.0",
"resolved": "https://registry.npmjs.org/chai/-/chai-3.4.1.tgz",
"integrity": "sha1-Mwri+BkSTCYYIDb6XkOojqThvYU=",
"dev": true,
"requires": {
"assertion-error": "1.0.1",
"deep-eql": "0.1.3",
"type-detect": "1.0.0"
},
"dependencies": {
"assertion-error": {
"version": "1.0.1",
"from": "assertion-error@>=1.0.1 <2.0.0",
"resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.0.1.tgz"
"resolved": "https://registry.npmjs.org/assertion-error/-/assertion-error-1.0.1.tgz",
"integrity": "sha1-NaruwzCX8R9COZ7K3zP6zNJ/XEw=",
"dev": true
},
"deep-eql": {
"version": "0.1.3",
"from": "deep-eql@>=0.1.3 <0.2.0",
"resolved": "https://registry.npmjs.org/deep-eql/-/deep-eql-0.1.3.tgz",
"integrity": "sha1-71WKyrjeJSBs1xOQbXTlaTDrafI=",
"dev": true,
"requires": {
"type-detect": "0.1.1"
},
"dependencies": {
"type-detect": {
"version": "0.1.1",
"from": "type-detect@0.1.1",
"resolved": "https://registry.npmjs.org/type-detect/-/type-detect-0.1.1.tgz"
"resolved": "https://registry.npmjs.org/type-detect/-/type-detect-0.1.1.tgz",
"integrity": "sha1-C6XsKohWQORw6k6FBZcZANrFiCI=",
"dev": true
}
}
},
"type-detect": {
"version": "1.0.0",
"from": "type-detect@>=1.0.0 <2.0.0",
"resolved": "https://registry.npmjs.org/type-detect/-/type-detect-1.0.0.tgz"
"resolved": "https://registry.npmjs.org/type-detect/-/type-detect-1.0.0.tgz",
"integrity": "sha1-diIXzAbbJY7EiQihKY6LlRIejqI=",
"dev": true
}
}
},
"mocha": {
"version": "2.3.4",
"from": "mocha@>=2.3.3 <3.0.0",
"resolved": "https://registry.npmjs.org/mocha/-/mocha-2.3.4.tgz",
"integrity": "sha1-himm+wRPLSJapLgaKuLQAWmesmY=",
"dev": true,
"requires": {
"commander": "2.3.0",
"debug": "2.2.0",
"diff": "1.4.0",
"escape-string-regexp": "1.0.2",
"glob": "3.2.3",
"growl": "1.8.1",
"jade": "0.26.3",
"mkdirp": "0.5.0",
"supports-color": "1.2.0"
},
"dependencies": {
"commander": {
"version": "2.3.0",
"from": "commander@2.3.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-2.3.0.tgz"
"resolved": "https://registry.npmjs.org/commander/-/commander-2.3.0.tgz",
"integrity": "sha1-/UMOiJgy7DU7ms0d4hfBHLPu+HM=",
"dev": true
},
"debug": {
"version": "2.2.0",
"from": "debug@2.2.0",
"resolved": "https://registry.npmjs.org/debug/-/debug-2.2.0.tgz",
"integrity": "sha1-+HBX6ZWxofauaklgZkE3vFbwOdo=",
"dev": true,
"requires": {
"ms": "0.7.1"
},
"dependencies": {
"ms": {
"version": "0.7.1",
"from": "ms@0.7.1",
"resolved": "https://registry.npmjs.org/ms/-/ms-0.7.1.tgz"
"resolved": "https://registry.npmjs.org/ms/-/ms-0.7.1.tgz",
"integrity": "sha1-nNE8A62/8ltl7/3nzoZO6VIBcJg=",
"dev": true
}
}
},
"diff": {
"version": "1.4.0",
"from": "diff@1.4.0",
"resolved": "https://registry.npmjs.org/diff/-/diff-1.4.0.tgz"
"resolved": "https://registry.npmjs.org/diff/-/diff-1.4.0.tgz",
"integrity": "sha1-fyjS657nsVqX79ic5j3P2qPMur8=",
"dev": true
},
"escape-string-regexp": {
"version": "1.0.2",
"from": "escape-string-regexp@1.0.2",
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.2.tgz"
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.2.tgz",
"integrity": "sha1-Tbwv5nTnGUnK8/smlc5/LcHZqNE=",
"dev": true
},
"glob": {
"version": "3.2.3",
"from": "glob@3.2.3",
"resolved": "https://registry.npmjs.org/glob/-/glob-3.2.3.tgz",
"integrity": "sha1-4xPusknHr/qlxHUoaw4RW1mDlGc=",
"dev": true,
"requires": {
"graceful-fs": "2.0.3",
"inherits": "2.0.1",
"minimatch": "0.2.14"
},
"dependencies": {
"graceful-fs": {
"version": "2.0.3",
"from": "graceful-fs@>=2.0.0 <2.1.0",
"resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-2.0.3.tgz"
"resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-2.0.3.tgz",
"integrity": "sha1-fNLNsiiko/Nule+mzBQt59GhNtA=",
"dev": true
},
"inherits": {
"version": "2.0.1",
"from": "inherits@>=2.0.0 <3.0.0",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.1.tgz"
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.1.tgz",
"integrity": "sha1-sX0I0ya0Qj5Wjv9xn5GwscvfafE=",
"dev": true
},
"minimatch": {
"version": "0.2.14",
"from": "minimatch@>=0.2.11 <0.3.0",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-0.2.14.tgz",
"integrity": "sha1-x054BXT2PG+aCQ6Q775u9TpqdWo=",
"dev": true,
"requires": {
"lru-cache": "2.7.3",
"sigmund": "1.0.1"
},
"dependencies": {
"lru-cache": {
"version": "2.7.3",
"from": "lru-cache@>=2.0.0 <3.0.0",
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-2.7.3.tgz"
"resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-2.7.3.tgz",
"integrity": "sha1-bUUk6LlV+V1PW1iFHOId1y+06VI=",
"dev": true
},
"sigmund": {
"version": "1.0.1",
"from": "sigmund@>=1.0.0 <1.1.0",
"resolved": "https://registry.npmjs.org/sigmund/-/sigmund-1.0.1.tgz"
"resolved": "https://registry.npmjs.org/sigmund/-/sigmund-1.0.1.tgz",
"integrity": "sha1-P/IfGYytIXX587eBhT/ZTQ0ZtZA=",
"dev": true
}
}
}
@ -99,42 +149,56 @@
},
"growl": {
"version": "1.8.1",
"from": "growl@1.8.1",
"resolved": "https://registry.npmjs.org/growl/-/growl-1.8.1.tgz"
"resolved": "https://registry.npmjs.org/growl/-/growl-1.8.1.tgz",
"integrity": "sha1-Sy3sjZB+k9szZiTc7AGDUC+MlCg=",
"dev": true
},
"jade": {
"version": "0.26.3",
"from": "jade@0.26.3",
"resolved": "https://registry.npmjs.org/jade/-/jade-0.26.3.tgz",
"integrity": "sha1-jxDXl32NefL2/4YqgbBRPMslaGw=",
"dev": true,
"requires": {
"commander": "0.6.1",
"mkdirp": "0.3.0"
},
"dependencies": {
"commander": {
"version": "0.6.1",
"from": "commander@0.6.1",
"resolved": "https://registry.npmjs.org/commander/-/commander-0.6.1.tgz"
"resolved": "https://registry.npmjs.org/commander/-/commander-0.6.1.tgz",
"integrity": "sha1-+mihT2qUXVTbvlDYzbMyDp47GgY=",
"dev": true
},
"mkdirp": {
"version": "0.3.0",
"from": "mkdirp@0.3.0",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.0.tgz"
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.3.0.tgz",
"integrity": "sha1-G79asbqCevI1dRQ0kEJkVfSB/h4=",
"dev": true
}
}
},
"mkdirp": {
"version": "0.5.0",
"from": "mkdirp@0.5.0",
"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.0.tgz",
"integrity": "sha1-HXMHam35hs2TROFecfzAWkyavxI=",
"dev": true,
"requires": {
"minimist": "0.0.8"
},
"dependencies": {
"minimist": {
"version": "0.0.8",
"from": "minimist@0.0.8",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz"
"resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",
"integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=",
"dev": true
}
}
},
"supports-color": {
"version": "1.2.0",
"from": "supports-color@1.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-1.2.0.tgz"
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-1.2.0.tgz",
"integrity": "sha1-/x7R5hFp0Gs88tWI4YixjYhH4X4=",
"dev": true
}
}
}

View file

@ -1,13 +1,13 @@
{
"name": "overleaf-error-type",
"version": "1.0.0",
"description": "Utility for creating custom error types.",
"description": "Make custom error types that pass `instanceof` checks, have stack traces and support custom messages and properties.",
"main": "index.js",
"scripts": {
"test": "mocha --require test/support"
},
"author": "team@overleaf.com",
"license": "Proprietary",
"author": "Overleaf (https://www.overleaf.com)",
"license": "MIT",
"private": true,
"devDependencies": {
"chai": "^3.3.0",