Skip to content

Instantly share code, notes, and snippets.

@cacheflow
Created August 18, 2018 17:43
Show Gist options
  • Save cacheflow/c762748320cc57fe7f9a5519472ad7dc to your computer and use it in GitHub Desktop.
Save cacheflow/c762748320cc57fe7f9a5519472ad7dc to your computer and use it in GitHub Desktop.
const errorCodes = require('./errorCodes');
const stringifyData = require('./stringifyData');
const handleError = (res) => {
let errMsg;
const options = {
showTwoFactorCode: false,
showVerifyCode: false,
status: 200,
}
const { error: { data: { code } } = {} } = JSON.parse(res.error);
switch(code) {
case errorCodes.login_auth:
errMsg = 'Incorrect Username or Password.'
return {
...options,
error: errMsg,
status: 422,
}
case errorCodes.login_verify_code:
errMsg = 'Invalid login verification code.'
return {
...options,
error: errMsg,
status: 422,
}
case errorCodes.login_twofactor_code:
errMsg = 'Invalid two factor code.'
return {
...options,
error: errMsg,
status: 422,
}
case errorCodes.login_verify_no_choice:
errMsg = 'No email or phone number associated with your account.'
return {
...options,
error: errMsg,
status: 422,
}
case errorCodes.login_access:
errMsg = 'Additional verification required'
return {
...options,
error: errMsg,
status: 200,
}
case errorCodes.login_twofactor:
errMsg = 'Enter the code Instagram sent to your phone or email.'
return {
...options,
showTwoFactorCode: true,
error: errMsg,
status: 200,
}
case errorCodes.login_verify:
errMsg = 'Enter the code Instagram sent to your phone or email.'
return {
...options,
showVerifyCode: true,
error: errMsg,
status: 200,
}
case errorCodes.login_verify_ok:
errMsg = 'Login verification complete. We need you to enter your password and email again'
return {
...options,
showVerifyCode: true,
error: errMsg,
}
case errorCodes.request_error:
errMsg = 'Invalid request. Try entering your username and password again.'
return {
...options,
showVerifyCode: true,
error: errMsg,
status: 422,
}
case errorCodes.login_access:
errMsg = 'Additional verfication(captcha) required. Enter the captcha you see on Instagram or that was texted/emailed to you.'
return {
...options,
showVerifyCode: true,
error: errMsg,
}
default:
return {
...error
}
}
}
module.exports = (e) => stringifyData(handleError(e))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment