Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // First, using .execSync():
- //
- var _ = require('lodash');
- var rendered;
- try {
- rendered = require('machinepack-strings').template({
- templateStr: undefined,
- data: []
- }).execSync()
- } catch (e) {
- console.log('Error! (exit: `%s`', e.exit);
- // If you want to handle any other exits, do it here.
- // e.g.
- if (e.exit === 'missingData') { /* ... */ }
- else if (e.exit === 'couldNotRender') { /* ... */ }
- // Anything else comes through the catchall "error" exit
- // (including validation errors)
- else {
- // One or more of the provided input values
- // failed validation.
- if (e.code === 'E_MACHINE_RUNTIME_VALIDATION') {
- console.log('\n\n');
- console.log('%d validation errors:', e.errors.length);
- _.each(e.errors, function (subErr,i){
- console.log('-----------------------\nERROR #%d',i);
- console.log('\nInput name:', subErr.input);
- console.log('\nDefault error message:', subErr.message);
- console.log('\nMore advanced details (probably wont need these):', subErr.code, require('util').inspect(subErr.errors, {depth: null}));
- // console.log(_.keys(subErr));
- console.log();
- });
- }
- // If code is not `E_MACHINE_RUNTIME_VALIDATION`, this is an unexpected error from the machine implementation itself.
- else {
- // Handle it however you're handling other unexpected errors in your package or application:
- // ...
- throw e;
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- //
- // Same thing, but with .exec():
- //
- var _ = require('lodash');
- require('machinepack-strings').template({
- templateStr: undefined,
- data: []
- }).exec({
- error: function (err) {
- // Validation errors always come through as "error":
- console.log('Error! (exit: `%s`', err.exit);
- // One or more of the provided input values
- // failed validation.
- if (err.code === 'E_MACHINE_RUNTIME_VALIDATION') {
- console.log('\n\n');
- console.log('%d validation errors:', err.errors.length);
- _.each(err.errors, function (subErr,i){
- console.log('-----------------------\nERROR #%d',i);
- console.log('\nInput name:', subErr.input);
- console.log('\nDefault error message:', subErr.message);
- console.log('\nMore advanced details (probably wont need these):', subErr.code, require('util').inspect(subErr.errors, {depth: null}));
- // console.log(_.keys(subErr));
- console.log();
- // ...
- });
- }
- // Otherwise, if this is not a validation error, handle it however you're handling other unexpected errors in your package or application:
- else {
- // ...
- }
- },
- //
- //
- // If you want to handle other exits, do it here; e.g.
- // missingData: function (info) { ... },
- // couldNotRender: function () { ... },
- //
- // Handle the success case where the template was rendered successfully
- success: function (rendered){
- // ...
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement