Ver código fonte

Merge pull request #4 from overleaf/ta-update

Update to OError
John Lees-Miller 7 anos atrás
pai
commit
5563844151

+ 10 - 10
libraries/o-error/README.md

@@ -8,17 +8,17 @@ Make custom error types that:
 
 ## For ES6
 
-ES6 classes make it easy to define custom errors by subclassing `Error`. Subclassing `errorType.Error` 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
 
 #### Throw an error directly
 
 ```js
-const errorType = require('overleaf-error-type')
+const OError = require('overleaf-error-type')
 
 function doSomethingBad () {
-  throw new errorType.Error({
+  throw new OError({
     message: 'did something bad',
     info: { thing: 'foo' }
   })
@@ -34,7 +34,7 @@ doSomethingBad()
 #### Custom error class
 
 ```js
-class FooError extends errorType.Error {
+class FooError extends OError {
   constructor (options) {
     super({ message: 'failed to foo', ...options })
   }
@@ -77,7 +77,7 @@ doFoo2()
 try {
   doFoo2()
 } catch (err) {
-  console.log(errorType.getFullStack(err))
+  console.log(OError.getFullStack(err))
 }
 // =>
 // 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
 
 ```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 () {
   throw new CustomError()
@@ -117,7 +117,7 @@ doSomethingBad()
 #### Define an error subclass
 
 ```js
-const SubCustomError = errorType.extend(CustomError, 'SubCustomError')
+const SubCustomError = OError.extend(CustomError, 'SubCustomError')
 
 try {
   throw new SubCustomError()
@@ -132,7 +132,7 @@ try {
 #### Add custom message and/or properties
 
 ```js
-const UserNotFoundError = errorType.define('UserNotFoundError',
+const UserNotFoundError = OError.define('UserNotFoundError',
   function (userId) {
     this.message = `User not found: ${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.userId = userId
 })

+ 10 - 26
libraries/o-error/index.js

@@ -22,7 +22,7 @@ var util = require('util')
  *
  * @extends Error
  */
-class ErrorTypeError extends Error {
+class OError extends Error {
   /**
    * @param {string} message as for built-in Error
    * @param {?object} info extra data to attach to the error
@@ -50,23 +50,6 @@ class ErrorTypeError extends Error {
   }
 }
 
-/**
- * Base class for errors with a corresponding HTTP status code.
- *
- * @extends ErrorTypeError
- */
-class ErrorWithStatusCode extends ErrorTypeError {
-  /**
-   * @param {?number} statusCode an HTTP status code
-   * @param {object} options as for ErrorTypeError
-   */
-  constructor ({ statusCode, ...options }) {
-    super(options)
-    this.statusCode = statusCode || 500
-  }
-}
-exports.ErrorWithStatusCode = ErrorWithStatusCode
-
 /**
  * Return the `info` property from `error` and recursively merge the `info`
  * properties from the error's causes, if any.
@@ -109,10 +92,9 @@ function hasCauseInstanceOf (error, klass) {
   return error instanceof klass || hasCauseInstanceOf(error.cause, klass)
 }
 
-exports.Error = ErrorTypeError
-exports.getFullInfo = getFullInfo
-exports.getFullStack = getFullStack
-exports.hasCauseInstanceOf = hasCauseInstanceOf
+OError.getFullInfo = getFullInfo
+OError.getFullStack = getFullStack
+OError.hasCauseInstanceOf = hasCauseInstanceOf
 
 //
 // For ES5
@@ -150,7 +132,9 @@ function defineErrorTypeIn (container, name, builder) {
   extendErrorTypeIn(container, Error, name, builder)
 }
 
-exports.extend = extendErrorType
-exports.define = defineErrorType
-exports.extendIn = extendErrorTypeIn
-exports.defineIn = defineErrorTypeIn
+OError.extend = extendErrorType
+OError.define = defineErrorType
+OError.extendIn = extendErrorTypeIn
+OError.defineIn = defineErrorTypeIn
+
+module.exports = OError

+ 9 - 34
libraries/o-error/test/error-type-class.test.js

@@ -1,19 +1,19 @@
-const errorType = require('..')
+const OError = require('..')
 const { expectError } = require('./support')
 
-class CustomError1 extends errorType.Error {
+class CustomError1 extends OError {
   constructor (options) {
     super({ message: 'failed to foo', ...options })
   }
 }
 
-class CustomError2 extends errorType.Error {
+class CustomError2 extends OError {
   constructor (options) {
     super({ message: 'failed to bar', ...options })
   }
 }
 
-describe('errorType.Error', () => {
+describe('OError', () => {
   it('handles a custom error type with a cause', () => {
     function doSomethingBadInternally () {
       throw new Error('internal error')
@@ -37,8 +37,8 @@ describe('errorType.Error', () => {
         message: 'CustomError1: failed to foo: internal error',
         firstFrameRx: /doSomethingBad/
       })
-      expect(errorType.getFullInfo(e)).to.deep.equal({ userId: 123 })
-      const fullStack = errorType.getFullStack(e)
+      expect(OError.getFullInfo(e)).to.deep.equal({ userId: 123 })
+      const fullStack = OError.getFullStack(e)
       expect(fullStack).to.match(
         /^CustomError1: failed to foo: internal error$/m
       )
@@ -79,11 +79,11 @@ describe('errorType.Error', () => {
         message: 'CustomError1: failed to foo: failed to bar: internal error',
         firstFrameRx: /doFoo/
       })
-      expect(errorType.getFullInfo(e)).to.deep.equal({
+      expect(OError.getFullInfo(e)).to.deep.equal({
         userId: 123,
         database: 'a'
       })
-      const fullStack = errorType.getFullStack(e)
+      const fullStack = OError.getFullStack(e)
       expect(fullStack).to.match(
         /^CustomError1: failed to foo: failed to bar: internal error$/m
       )
@@ -101,34 +101,9 @@ describe('errorType.Error', () => {
       throw new CustomError1({})
       expect.fail('should have thrown')
     } catch (e) {
-      expect(errorType.getFullInfo(e)).to.deep.equal({})
+      expect(OError.getFullInfo(e)).to.deep.equal({})
       let infoKey = Object.keys(e).find(k => k === 'info')
       expect(infoKey).to.not.exist
     }
   })
 })
-
-describe('errorType.ErrorWithStatusCode', () => {
-  it('accepts a status code', () => {
-    function findPage () {
-      throw new errorType.ErrorWithStatusCode({
-        message: 'page not found',
-        info: { url: '/foo' },
-        statusCode: 404
-      })
-    }
-
-    try {
-      findPage()
-    } catch (e) {
-      expectError(e, {
-        name: 'ErrorWithStatusCode',
-        klass: errorType.ErrorWithStatusCode,
-        message: 'ErrorWithStatusCode: page not found',
-        firstFrameRx: /findPage/
-      })
-      expect(e.statusCode).to.equal(404)
-      expect(e.info).to.deep.equal({url: '/foo'})
-    }
-  })
-})

+ 3 - 3
libraries/o-error/test/error-type-util.test.js

@@ -1,6 +1,6 @@
 const { getFullInfo, getFullStack, hasCauseInstanceOf } = require('..')
 
-describe('errorType.getFullInfo', () => {
+describe('OError.getFullInfo', () => {
   it('works on a normal error', () => {
     const err = new Error('foo')
     expect(getFullInfo(err)).to.deep.equal({ })
@@ -43,7 +43,7 @@ describe('errorType.getFullInfo', () => {
   })
 })
 
-describe('errorType.getFullStack', () => {
+describe('OError.getFullStack', () => {
   it('works on a normal error', () => {
     const err = new Error('foo')
     const fullStack = getFullStack(err)
@@ -63,7 +63,7 @@ describe('errorType.getFullStack', () => {
   })
 })
 
-describe('errorType.hasCauseInstanceOf', () => {
+describe('OError.hasCauseInstanceOf', () => {
   it('works on a normal error', () => {
     const err = new Error('foo')
     expect(hasCauseInstanceOf(null, Error)).to.be.false

+ 9 - 9
libraries/o-error/test/error-type.test.js

@@ -1,11 +1,11 @@
 'use strict'
 
-var errorType = require('..')
+var OError = require('..')
 const { expectError } = require('./support')
 
-describe('errorType', function () {
+describe('OError', function () {
   it('defines a custom error type', function () {
-    var CustomError = errorType.define('CustomError')
+    var CustomError = OError.define('CustomError')
 
     function doSomethingBad () {
       throw new CustomError()
@@ -25,7 +25,7 @@ describe('errorType', function () {
   })
 
   it('defines a custom error type with a message', function () {
-    var CustomError = errorType.define('CustomError', function (x) {
+    var CustomError = OError.define('CustomError', function (x) {
       this.message = 'x=' + x
       this.x = x
     })
@@ -45,8 +45,8 @@ describe('errorType', function () {
   })
 
   it('defines extended error type', function () {
-    var BaseError = errorType.define('BaseError')
-    var DerivedError = errorType.extend(BaseError, 'DerivedError')
+    var BaseError = OError.define('BaseError')
+    var DerivedError = OError.extend(BaseError, 'DerivedError')
 
     function doSomethingBad () {
       throw new DerivedError()
@@ -62,7 +62,7 @@ describe('errorType', function () {
 
   it('defines error types in a container object', function () {
     var SomeClass = {}
-    errorType.defineIn(SomeClass, 'CustomError')
+    OError.defineIn(SomeClass, 'CustomError')
 
     function doSomethingBad () {
       throw new SomeClass.CustomError()
@@ -78,11 +78,11 @@ describe('errorType', function () {
 
   it('extends error types in a container object', function () {
     var SomeClass = {}
-    errorType.defineIn(SomeClass, 'CustomError', function (payload) {
+    OError.defineIn(SomeClass, 'CustomError', function (payload) {
       this.message = 'custom error'
       this.payload = payload
     })
-    errorType.extendIn(SomeClass, SomeClass.CustomError, 'DerivedCustomError',
+    OError.extendIn(SomeClass, SomeClass.CustomError, 'DerivedCustomError',
       function (payload) {
         SomeClass.CustomError.call(this, payload)
         this.message = 'derived custom error'