Skip to content

How to change the default error output in restify 7.x? #93

Open

Description

@qburst-ranjith

I changed the default error output in restify 4.x using the below code

`var restify = require('restify');
var util = require('util');

function ValidationError(message, errors) {
restify.RestError.call(this, {
restCode: 'ValidationError',
statusCode: 400,
message: message,
constructorOpt: ValidationError
});
this.name = 'ValidationError';
this.body.errors = errors; //<---
}

util.inherits(ValidationError, restify.RestError);`

And I got the desired result. Now am in a process of migrating my project to restify 7.x. So I couldn't use the restify.RestError, I have tried 'restify-errors-options' package's subclassing property but it doesn't work for me. I have tried the below code
restifyErrors.makeConstructor('ValidationError', { restCode: 'ValidationError', statusCode: 400, message: message }); var myErr = new errors.ValidationError( );

And I ended up with the below error

var myErr = new errors.ValidationError(); ^

TypeError: errors.ValidationError is not a constructor

So can anyone suggest a solution for this problem?

Activity

  1. dvdkon commented on Jan 27, 2019

    @dvdkon

    It seems that the docs are wrong, makeConstructor actually returns the new constructor instead of storing it in the module, so the proper way to use it would be

    let ValidationError = restifyErrors.makeConstructor('ValidationError', {
        restCode: 'ValidationError',
        statusCode: 400,
        message: message
    });
    var myErr = new errors.ValidationError();
    
  2. SensitiveMix commented on Apr 1, 2019

    @SensitiveMix

    Restify-errors 7.x makeConstructor doesn't work, I use it normally in 5.x. You can install Restify 7.2.1.

  3. SensitiveMix commented on Apr 1, 2019

    @SensitiveMix
  4. sephiroth32 commented on Apr 2, 2019

    @sephiroth32

    in older versions the plugin add the constructor, in this version you need add manually

    errs.testerror = errs.makeConstructor('testerror', {
        statusCode: 200,
        message: 'my default message'
    });
    
    
    
  5. DonutEspresso commented on Apr 4, 2019

    @DonutEspresso
    Member

    Hi all - what version of restify-errors are you using? This should work on latest 7.0.0 release:

    const errs = require('restify-errors');
    const MyErr = errs.makeConstructor('MyError');
    throw new MyErr('boom');
    /Users/aliu/Sandbox/test/errors.js:3
    throw new MyErr('boom');
    ^
    MyError: boom
        at Object.<anonymous> (/Users/aliu/Sandbox/test/errors.js:3:7)
        at Module._compile (internal/modules/cjs/loader.js:701:30)
        at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
        at Module.load (internal/modules/cjs/loader.js:600:32)
        at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
        at Function.Module._load (internal/modules/cjs/loader.js:531:3)
        at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
        at startup (internal/bootstrap/node.js:283:19)
        at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
    

    Regarding the storing of classes on the exports of the module, that feature was removed in 6.x. Some additional context can be found here:
    https://github.com/restify/errors#custom-constructors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions