all files / express-stormpath/lib/okta/ error-transformer.js

69.23% Statements 9/13
75% Branches 9/12
100% Functions 2/2
69.23% Lines 9/13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28                                     
'use strict';
 
/**
 * Translates user creation errors into an end-user friendly userMessage
 * @param {*} err
 */
function oktaErrorTransformer(err) {
  if (err && err.errorCauses) {
    err.errorCauses.forEach(function (cause) {
      Iif (cause.errorSummary === 'login: An object with this field already exists in the current organization') {
        err.userMessage = 'An account with that email address already exists.';
      } else Eif (!err.userMessage) {
        // This clause allows the first error cause to be returned to the user
        err.userMessage = cause.errorSummary;
      }
    });
  }
 
  Iif (err && err.message === 'Invalid grant') {
    err.status = 400;
    err.code = 7104;
    err.message = 'Invalid username or password.';
  }
 
  return err;
}
 
module.exports = oktaErrorTransformer;