totaljs/framework4

Is there a way to change the default ErrorBuilder Object Structure?

LandyCuadra opened this issue · 2 comments

Is the feature request related to a problem? Please describe.
I am trying to customize the error response by adding/changing some keys to it:

this is the default error object

{
        name: 'String: error-name-or-error-code',
        error: 'String: error description',
        path: 'String: absolute path to invalid variable',
        index: Number // optional, array index if some item in array is invalid
}

Describe the solution you'd like
would to know a way to add fields, that can be invoke with $.invalid()

{
        code: 'Number: Error code',
        name: 'String: error-name-or-error-code',
        message: 'String: error description',
        show: 'Boolean: flag to show or not error message',
}

Describe alternatives you've considered
I have tried using the ErrorBuilder AddTransform function, to rename the fields....
but but it is kind of messy to assign builder['show'] = Boolean(err.path)... also I am limited to the quantity of fields of the default object

How do you imagine to assign .show field? From where? Where do you want to specify it?

  • $.invalid() method contains fixed arguments.You can't change them - or you may change them, but I don't recommend it
  • I recommend to working with specified fields in the ErrorBuilder

BTW: you can extend error builder instance dynamically in the schemas like this:

$.error. // --> is ErrorBuilder instance
$.error.visible = { errorid1: true, errorid2: true };

Then you can use it in transformation like this:

ErrorBuilder.addTransform('custom', function(isresponse) {
    this.visible. // your data
    for (var err of this.items)
        err.show = this.visible[err.name];
}, true);

You suggestion was correct, I was misunderstanding the function of error builder.... Finally I solved it with $.success(false, ErrorObject)... Thank you