Skip to content

Latest commit

 

History

History
42 lines (32 loc) · 2.06 KB

README.md

File metadata and controls

42 lines (32 loc) · 2.06 KB

message-catalog-manager

Build Status Coverage Status

Overview

This module provides a mechanism for translating messages to provide user readable information. Messages support named and positional inserts to give context to the messages. Messages can include messages and actions, as well as urls for more information. A catloggedError class carries catalog message context information so errors thrown around a system can be translated at the appropriate point.

Catalog file format

The catalog manager works with a set of json message catalog files contain message text.
These are indexed in a separate catalog index file which is provided to the catalog manager.

For an example of the catalog file format see:
messages.json
For an example of the catalog index fileformat see:
catalog-index.json

Using messageCatalogManager

var MessageCatalogManager = require("message-catalog").messageCatalogManager;
var catalogManager = new MessageCatalogManager("/catalog-index.json");
var formattedMessage = catalogManager.getMessage("catalog1", "0001", {}, ["myapp"]);

Using catalogedError

var CatalogedError = require("message-catalog").catalogedError;
var throw new CatalogedError("0001", "catalog1", "An error occurred", ["myapp"]);

Express Middleware

If your Express application generates or receives a catalogedError you can use the ErrorFormattingMiddleware middleware to intercept all responses and attempt to format error responses before they are sent.

app.use(new ErrorFormattingMiddleware('catalog-index.json'));

There is a simple application that always responds with a catalogedError in example. Start it like

node example/errorMiddlewareApp/errorMiddlewareApp.js