change exports

This commit is contained in:
Tim Alby
2019-07-05 18:45:43 +02:00
parent 626e6267ae
commit 9afd8e4e5d
2 changed files with 19 additions and 18 deletions
+10 -10
View File
@@ -8,17 +8,17 @@ Make custom error types that:
## For ES6 ## For ES6
ES6 classes make it easy to define custom errors by subclassing `Error`. Subclassing `OErrors.OError` adds a few extra helpers. ES6 classes make it easy to define custom errors by subclassing `Error`. Subclassing `OError` adds a few extra helpers.
### Usage ### Usage
#### Throw an error directly #### Throw an error directly
```js ```js
const OErrors = require('overleaf-error-type') const OError = require('overleaf-error-type')
function doSomethingBad () { function doSomethingBad () {
throw new OErrors.OError({ throw new OError({
message: 'did something bad', message: 'did something bad',
info: { thing: 'foo' } info: { thing: 'foo' }
}) })
@@ -34,7 +34,7 @@ doSomethingBad()
#### Custom error class #### Custom error class
```js ```js
class FooError extends OErrors.OError { class FooError extends OError {
constructor (options) { constructor (options) {
super({ message: 'failed to foo', ...options }) super({ message: 'failed to foo', ...options })
} }
@@ -77,7 +77,7 @@ doFoo2()
try { try {
doFoo2() doFoo2()
} catch (err) { } catch (err) {
console.log(OErrors.getFullStack(err)) console.log(OError.getFullStack(err))
} }
// => // =>
// FooError: failed to foo: bad // FooError: failed to foo: bad
@@ -101,9 +101,9 @@ The approach is based mainly on https://gist.github.com/justmoon/15511f92e5216fa
#### Define a standalone error class #### Define a standalone error class
```js ```js
const errorType = require('overleaf-error-type') const OError = require('overleaf-error-type')
const CustomError = errorType.define('CustomError') const CustomError = OError.define('CustomError')
function doSomethingBad () { function doSomethingBad () {
throw new CustomError() throw new CustomError()
@@ -117,7 +117,7 @@ doSomethingBad()
#### Define an error subclass #### Define an error subclass
```js ```js
const SubCustomError = errorType.extend(CustomError, 'SubCustomError') const SubCustomError = OError.extend(CustomError, 'SubCustomError')
try { try {
throw new SubCustomError() throw new SubCustomError()
@@ -132,7 +132,7 @@ try {
#### Add custom message and/or properties #### Add custom message and/or properties
```js ```js
const UserNotFoundError = errorType.define('UserNotFoundError', const UserNotFoundError = OError.define('UserNotFoundError',
function (userId) { function (userId) {
this.message = `User not found: ${userId}` this.message = `User not found: ${userId}`
this.userId = userId this.userId = userId
@@ -151,7 +151,7 @@ class User {
} }
} }
errorType.defineIn(User, 'UserNotFoundError', function (userId) { OError.defineIn(User, 'UserNotFoundError', function (userId) {
this.message = `User not found: ${userId}` this.message = `User not found: ${userId}`
this.userId = userId this.userId = userId
}) })
+9 -8
View File
@@ -92,10 +92,9 @@ function hasCauseInstanceOf (error, klass) {
return error instanceof klass || hasCauseInstanceOf(error.cause, klass) return error instanceof klass || hasCauseInstanceOf(error.cause, klass)
} }
exports.OError = OError OError.getFullInfo = getFullInfo
exports.getFullInfo = getFullInfo OError.getFullStack = getFullStack
exports.getFullStack = getFullStack OError.hasCauseInstanceOf = hasCauseInstanceOf
exports.hasCauseInstanceOf = hasCauseInstanceOf
// //
// For ES5 // For ES5
@@ -133,7 +132,9 @@ function defineErrorTypeIn (container, name, builder) {
extendErrorTypeIn(container, Error, name, builder) extendErrorTypeIn(container, Error, name, builder)
} }
exports.extend = extendErrorType OError.extend = extendErrorType
exports.define = defineErrorType OError.define = defineErrorType
exports.extendIn = extendErrorTypeIn OError.extendIn = extendErrorTypeIn
exports.defineIn = defineErrorTypeIn OError.defineIn = defineErrorTypeIn
module.exports = OError