apollo-errors icon indicating copy to clipboard operation
apollo-errors copied to clipboard

How to pass variables into the error message.

Open santanuyogi opened this issue 5 years ago • 2 comments

This is more of a question than it is an issue. Is there any way to pass a variable into the error message?

If I have the following error message:

throw new Error(
  `There was no user found with the username, ${username}.`
);

How can I get that username into the apollo-errors? Is there a way to pass it as an argument?

throw new err.IncorrectUsernameError(username);

Inside my error file that imports apollo-errors:

exports.IncorrectUsernameError = createError('IncorrectPasswordError', {
  message: `There was no user found with the username, ${username}.`,
});

Please let me know if there is some way to accomplish this.

santanuyogi avatar Apr 16 '20 17:04 santanuyogi

Hi, just found this library, but I think you could achieve that behavior using a HOC to wrap the error creation, that way you could send a custom message, maybe it's too late for the reply but someone might find it useful

whitte-h avatar Feb 14 '21 20:02 whitte-h

Also, you could just overload the message as it's in the docs

whitte-h avatar Feb 14 '21 22:02 whitte-h