Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. //
  2. // First, using .execSync():
  3. //
  4.  
  5. var _ = require('lodash');
  6.  
  7. var rendered;
  8. try {
  9. rendered = require('machinepack-strings').template({
  10. templateStr: undefined,
  11. data: []
  12. }).execSync()
  13. } catch (e) {
  14. console.log('Error! (exit: `%s`', e.exit);
  15.  
  16. // If you want to handle any other exits, do it here.
  17. // e.g.
  18. if (e.exit === 'missingData') { /* ... */ }
  19. else if (e.exit === 'couldNotRender') { /* ... */ }
  20. // Anything else comes through the catchall "error" exit
  21. // (including validation errors)
  22. else {
  23.  
  24. // One or more of the provided input values
  25. // failed validation.
  26. if (e.code === 'E_MACHINE_RUNTIME_VALIDATION') {
  27. console.log('\n\n');
  28. console.log('%d validation errors:', e.errors.length);
  29. _.each(e.errors, function (subErr,i){
  30. console.log('-----------------------\nERROR #%d',i);
  31. console.log('\nInput name:', subErr.input);
  32. console.log('\nDefault error message:', subErr.message);
  33. console.log('\nMore advanced details (probably wont need these):', subErr.code, require('util').inspect(subErr.errors, {depth: null}));
  34. // console.log(_.keys(subErr));
  35. console.log();
  36. });
  37. }
  38. // If code is not `E_MACHINE_RUNTIME_VALIDATION`, this is an unexpected error from the machine implementation itself.
  39. else {
  40. // Handle it however you're handling other unexpected errors in your package or application:
  41. // ...
  42. throw e;
  43. }
  44.  
  45. }
  46. }
  47.  
  48.  
  49.  
  50. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  51.  
  52. //
  53. // Same thing, but with .exec():
  54. //
  55.  
  56. var _ = require('lodash');
  57.  
  58. require('machinepack-strings').template({
  59. templateStr: undefined,
  60. data: []
  61. }).exec({
  62. error: function (err) {
  63. // Validation errors always come through as "error":
  64. console.log('Error! (exit: `%s`', err.exit);
  65.  
  66. // One or more of the provided input values
  67. // failed validation.
  68. if (err.code === 'E_MACHINE_RUNTIME_VALIDATION') {
  69. console.log('\n\n');
  70. console.log('%d validation errors:', err.errors.length);
  71. _.each(err.errors, function (subErr,i){
  72. console.log('-----------------------\nERROR #%d',i);
  73. console.log('\nInput name:', subErr.input);
  74. console.log('\nDefault error message:', subErr.message);
  75. console.log('\nMore advanced details (probably wont need these):', subErr.code, require('util').inspect(subErr.errors, {depth: null}));
  76. // console.log(_.keys(subErr));
  77. console.log();
  78. // ...
  79. });
  80. }
  81. // Otherwise, if this is not a validation error, handle it however you're handling other unexpected errors in your package or application:
  82. else {
  83. // ...
  84. }
  85. },
  86. //
  87. //
  88. // If you want to handle other exits, do it here; e.g.
  89. // missingData: function (info) { ... },
  90. // couldNotRender: function () { ... },
  91. //
  92. // Handle the success case where the template was rendered successfully
  93. success: function (rendered){
  94. // ...
  95. }
  96. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement