1.0.11 • Published 5 years ago

that-koa-error v1.0.11

Weekly downloads
178
License
MIT
Repository
github
Last release
5 years ago

That Koa Error

CircleCI

Overview

A lightweight and extendable Error Handler library for Koa Applications

When any part of the application throws an Errors the error middleware will intercept it and gracefully respond to the user

Any Error which extends the ApplicationError class will be handled such that responses are properly wrapped with status codes etc.

All non-application errors are treated as 500 Internal Server Errrors

All errors will be logged to the console

Attaching Middleware

The below TYPESCRIPT provides an example of attaching the library to the Koa Application

import * as Koa from 'koa'
import { error } from 'that-koa-error'

const app = new Koa()

app.use(error())

Throwing Application Errors

The below TYPESCRIPT provides an example of how to throw an Application Error.

Please note: the ApplicationError class itself is abstract - so we can only throw an extension class. The basic extension is the GeneralError

some code...

if (something bad happened) {
    throw new GeneralError('A message to only display in console', arguments)
}

some more code...

assert Application Errors

A handy utility for quickly and cleanly throwing GeneralErrors via assertion

function myFunction(a: string, b: string) {
    assert(a, 'A must be provided')
    assert(
        b.length === a.length,
        'B must be the same length as A',
        {a, b}  // Pass on useful debug info here
    )
}

expectAsyncToThrow

A handy utility for testing

it('should throw an error in an async function', async() {
    await expectAsyncToThrow(async () => {
        throw new Error('Something happened')
    })
})
// This will be PASS


it('should throw an error in an async function', async() {
    await expectAsyncToThrow(async () => {
        return Promise.resolve()
    })
})
// This will be FAIL
1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

0.0.30

6 years ago

0.0.29

6 years ago

0.0.28

6 years ago

0.0.27

6 years ago

0.0.26

6 years ago

0.0.25

6 years ago

0.0.24

6 years ago

0.0.23

6 years ago

0.0.22

6 years ago

0.0.21

6 years ago

0.0.20

6 years ago

0.0.19

6 years ago

0.0.18

6 years ago

0.0.17

6 years ago

0.0.16

6 years ago

0.0.15

6 years ago

0.0.14

6 years ago

0.0.13

6 years ago

0.0.12

6 years ago

0.0.11

6 years ago

0.0.10

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago