A custom error message parser on narando architecture.
npm install @narando/notifications-parserA custom error message parser on narando architecture.
You need to have nodejs and npm installed.
``bash`
$ npm install @narando/notifications-parser
Parse errors witch were send via the url queries or created as a custom object. The object structure looks like:
`javascript
// Permission
{
// Name of error or could be an Array of names
"errors": "An error occured",
// Success message can be set while some errors exists.
"success": true
}
`
To create an instance of the notifications-parser module you have to import notifications-parser
`javascript
import notificationsParser from "@narando/notifications-parser";
// You can preconfigure custom error messages.
// If you don't specify custom messages the default messages will be used.
// The custom messages can expand or replace the existing messages.
//
// Example url:
// http://localhost/path?success=true&error=ERRORTYPE1&error=ERRORTYPE2
function getArticles(req, res) {
const customMessage = {
ERRORTYPE1: "custom message 1",
ERRORTYPE1: "custom message 2"
};
// Now you can use the parser to set your locals which will be send to your
// template engine.
res.locals.notifications = notificationsParser(req.query, customMessage);
// Returned Object:
// {
// success: "Success!",
// errors: "["custom Message 1", "custom message 2"]"
// }
res.render(page/article, res.locals);`
}
Use the notifications-parser without the custom messages
`javascript`
// Example url:
// http://localhost/path?success=true&error=ResourceMissingError
res.locals.notifications = notificationsParser(req.query);
// Returned Object:
// {
// success: "Success!",
// errors: "["custom Message 1", "Resource is missing!"]"
// }
}
The messages will be displayed by the following mustache logic.
In this case it will display all error messages and a potential success message.
`
{{#notifications}}
Flashing some notifications
In case you need to add a new error that is not set in the query you can use the following method.
The function will add a new error to the object with the errors which will be parsed by the notificationsParser
`javascript
import notificationsParser, {
flashNotification
} from "@narando/notifications-parser";// To add a new error to the query.error or your custom object
// You have to use flashNotification.error();
// This function adds the new error to the object.
const newError = "NewError";
flashNotification.error(req.query, newError);
res.locals.notifications = notificationsParser(req.query);
`L10n notifications
The default messages are available in German and English (US).
The l10n parameter is optional and set to English (US) by default.
Please use the BCP 47 language codes
$3
`javascript
import notificationsParser from "@narando/notifications-parser";function getArticles(req, res) {
// Example url:
// http://localhost/path?success=true&error=ResourceMissingError
res.locals.notifications = notificationsParser(req.query, {}, "de-DE");
// Returned Object:
// {
// success: "Aktion erfolgreich ausgeführt!",
// errors: ["Das angefragte Objekt fehlt."]
// }
res.render(
page/article, res.locals);
}
`$3
`javascript
import notificationsParser from "@narando/notifications-parser";function getArticles(req, res) {
// Get the custom messages from your l10n function.
const customMessage = {
ERRORTYPE1: "Spezifische Nachricht 1",
ERRORTYPE1: "Spezifische Nachricht 2"
};
// Example url:
// http://localhost/path?success=true&error=ERRORTYPE1&error=ERRORTYPE2
res.locals.notifications = notificationsParser(
req.query,
customMessage,
"de-DE"
);
// Returned Object:
// {
// success: "Aktion erfolgreich ausgeführt!",
// errors: "["Spezifische Nachricht 1", "Spezifische Nachricht 2"]"
// }
res.render(
page/article, res.locals);
}
``