How do I get programmatic error codes from Cognito?

Viewed 2416

I'm using the Cognito Javscript SDK, and I've created a form where a user can register an account. If, for whatever reason, server-side validation fails, the response looks like this:

{
  "__type":"InvalidParameterException",
  "message":"4 validation errors detected: Value at 'password' failed to satisfy constraint: Member must have length greater than or equal to 6; Value at 'password' failed to satisfy constraint: Member must satisfy regular expression pattern: [\\S]+; Value at 'username' failed to satisfy constraint: Member must have length greater than or equal to 1; Value at 'username' failed to satisfy constraint: Member must satisfy regular expression pattern: [\\p{L}\\p{M}\\p{S}\\p{N}\\p{P}]+"
}

The problem with this response is that I can't provide good user feedback because I'd have to parse the response to determine which fields need to be fixed. Is there a way to get the errors back in a format that is better for working with programmatically?

3 Answers

You have to first json stringify and then json parse the object to get values.

Try following sample code -

var errorObj = JSON.parse(JSON.stringify(data));
console.log(errorObj.statusCode);

Related