donejs-error-format

1.2.0 • Public • Published

donejs-error-format

Build Status npm version

An error formatter for Errors that are emitted by done-ssr.

donejs-error-format example

Install

npm install donejs-error-format --save

Usage

If you are using done-serve, it already uses donejs-error-format internally. If you use done-ssr or done-ssr-middleware, you can use this module to format your error messages.

done-ssr

const errorFormat = require("donejs-error-format");
const ssr = require("done-ssr");
 
const render = ssr({ config: __dirname + "/package.json" });
 
function app(request, response) {
    // More stuff here, obviously, like static assets, etc.
 
    let stream = render(request);
 
    stream.on("error", function(error){
        let parts = errorFormat.extract(error);
        let html = errorFormat.html(parts);
 
        console.error(error);
 
        response.writeHead(200, { type: "text/html" });
        response.end(html);
    });
 
    stream.pipe(response);
}
 
require("http").createServer(app).listen(8080);

done-ssr-middleware

const express = require("express");
const errorFormat = require("donejs-error-format");
const ssr = require("done-ssr-middleware");
 
const app = express();
 
app.use(express.static(__dirname + "/public"));
 
app.use(ssr({ config: __dirname + "/package.json!npm" }));
 
 
// The last middleware should be the error handler
app.use(function(error, request, response, next) {
    let parts = errorFormat.extract(error);
    let html = errorFormat.html(parts);
 
    console.error(error);
 
    response.type("html").end(html);
});

API

.extract(error)

This function takes an Error and returns an object with parts extracted. This is used to pass into .html() and other formatting functions (currently there is only HTML).

.html(parts)

This function is used to generate formatted HTML. It takes a parts object that comes from using .extract.

let parts = errorFormat.extract(error);
let html = errorFormat.html(parts);

.html(parts, options)

The second signature is like the first but takes an options object. The options are:

  • liveReload: This can either be the boolean true or an object that provides the port like: { port: 4044 }. By default the port 8012 is used (which is the default in DoneJS apps). You only need to set this option if you are using an alternative port in your development server.

Enabling the live-reload script:

let parts = errorFormat.extract(error);
let html = errorFormat.html(parts, {
    liveReload: true
})

Or with a port:

let parts = errorFormat.extract(error);
let html = errorFormat.html(parts, {
    liveReload: {
        port: 4044
    }
})

License

MIT

/donejs-error-format/

    Package Sidebar

    Install

    npm i donejs-error-format

    Weekly Downloads

    49

    Version

    1.2.0

    License

    ISC

    Unpacked Size

    19 kB

    Total Files

    15

    Last publish

    Collaborators

    • matthewp