cosmo-koa-errors

Cosmo Koa Error Handler

Usage no npm install needed!

<script type="module">
  import cosmoKoaErrors from 'https://cdn.skypack.dev/cosmo-koa-errors';
</script>

README

cosmo-koa-error

Handles application errors in a central place.

Usage

Install

npm install --save cosmo-koa-errors

setup middleware

const koa        = require('koa');

let app = koa();

// HERE: enable error captures
app.use(require('cosmo-koa-errors')());

// ... setups ... 

app.listen(3000);

throw errors in generator

function *() { 
  
  // param input: string
  // error output: 
  // { error: { message: "record not found" } }
  this.throw("record not found");
  
  // param input: object with message key
  // error output: 
  // { error: { message: "record not found" }}
  this.throw({ message: "record not found", otherKey: "blah" });
  
  // param input: object without message key
  // error output:
  // { error: { hello: "world" } }
  this.throw({ hello: "world"} )

  // Custom Error Status Code:
  this.throw('Error Message', 500);
    
}