How do I transform ErrorBuilder output?

Mon Jan 29 2018 09:22:49 GMT+0100 (CET), Peter Širka

How do I transform ErrorBuilder output?

  • Documentation for ErrorBuilder.addTransform()
  • create a definition file e.g. /definitions/transformations.js
ErrorBuilder.addTransform('toObject', function(isResponse) {

    var builder = {};

    for (var i = 0, length = this.items.length; i < length; i++) {
        var err = this.items[i];
        // err.name
        // err.error
        // err.path
        // err.index
        builder[err.name] = err.error;
    }

    // HTTP status (if the schema is used in the routing)
    this.status = 400;

    // Returns a new transformed object
    return isResponse ? JSON.stringify(builder) : builder;
});

// Sets the new transformation as a default output for all ErrorBuilders
ErrorBuilder.setDefaultTransform('toObject');

Tests:

var err = new ErrorBuilder();
err.push('email', 'Email is not valid');
err.push('age', 'You are too young');
err.push('something'); // error message will be read from a resource filfe
console.log(err.output());
// { email: 'Email is not valid', age: 'You are too young' }

Can I create multiple transformations?

Yes. You can create unlimited count of transformations.

ErrorBuilder.addTransform('csv', function() {
   // ...
});

ErrorBuilder.addTransform('xml', function() {
   // ...
});

Usage:

var err = new ErrorBuilder();
err.push('email', 'Email is not valid');
err.push('age', 'You are too young');
err.setTransform('xml');

// OR

var err = new ErrorBuilder();
err.push('email', 'Email is not valid');
err.push('age', 'You are too young');
err.setTransform('csv');

Tags

Follow us

Latest blogs
How do I transform ErrorBuilder output?
Mon Jan 29 2018 09:22:49 GMT+0100 (CET)
Flow: REST API components
Mon Jan 22 2018 10:11:14 GMT+0100 (CET)
New Total.js Eshop v11
Fri Jan 19 2018 09:55:09 GMT+0100 (CET)
New Total.js CMS v11
Fri Jan 19 2018 00:24:05 GMT+0100 (CET)
Flow 4.1.0
Thu Jan 18 2018 23:58:37 GMT+0100 (CET)

Latest comments
Total.js is amazing! ;)
Leonardo Hessel
Tue Dec 19 2017 19:51:15 GMT+0100 (CET)
It's easy, just set e.g. `res.status = 404`.
Peter
Thu Nov 16 2017 07:54:11 GMT+0100 (CET)
Yes, it's valid.
Peter
Thu Nov 16 2017 07:53:06 GMT+0100 (CET)
How do we set the status code in custome middleware?
Nabeel
Mon Oct 02 2017 10:31:40 GMT+0200 (CEST)
Node... Awesome applications.
oscar
Thu Aug 31 2017 02:21:59 GMT+0200 (CEST)
Awesome!
Tema
Wed May 10 2017 06:33:13 GMT+0200 (CEST)
Awesome, thanks!
Tema
Tue May 02 2017 14:13:03 GMT+0200 (CEST)