Charles Holbrow
Charles Holbrow

Reputation: 4231

Specify a sanitized error when a Meteor check fails

I using check to verify arguments to my meteor methods. I would like to send a helpful error message when the check fails.

Is there a way to specify a sanitized error to send to the client when the check fails?

I could wrap the check in a try/catch block and generate another Meteor error, but this seems needlessly verbose.

Upvotes: 0

Views: 298

Answers (1)

Alex Lapa
Alex Lapa

Reputation: 1159

The only way to do it I figured so far is to put your check inside of try catch block and then throw new exception with reason/message taken from original one. Not perfect, even ugly, but works for now.

try {
  check(param, String)
} catch (ex) {
  let message = ex.message.startsWith('Match error: Match error: ')
    ? ex.message.slice(26, ex.message.length)
    : ex.message

  throw new Meteor.Error(400, message)
}

Upvotes: 1

Related Questions