From fb782cebcfe957cc7b2e0642b4ea9d8c3d995930 Mon Sep 17 00:00:00 2001 From: Vesa Luusua Date: Tue, 17 Oct 2017 19:01:57 +0300 Subject: [PATCH] storableError function added --- src/util/errors.js | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/src/util/errors.js b/src/util/errors.js index affa18dc..3b9981ad 100644 --- a/src/util/errors.js +++ b/src/util/errors.js @@ -15,16 +15,23 @@ const ERROR_CODE_EMAIL_TAKEN = 'email-taken'; const ERROR_CODE_TOO_MANY_VERIFICATION_REQUESTS = 'email-too-many-verification-requests'; const ERROR_CODE_UPLOAD_OVER_LIMIT = 'request-upload-over-limit'; -const responseErrors = apiError => { - return apiError && apiError.data && apiError.data.errors ? apiError.data.errors : []; +const errorAPIErrors = error => { + return error && error.apiErrors ? error.apiErrors : []; }; -const hasErrorWithCode = (apiError, code) => { - return responseErrors(apiError).some(error => { - return error.code === code; +const hasErrorWithCode = (error, code) => { + return errorAPIErrors(error).some(apiError => { + return apiError.code === code; }); }; +/** + * return apiErrors from error response + */ +const responseAPIErrors = error => { + return error && error.data && error.data.errors ? error.data.errors : []; +}; + /** * Check if the given API error (from `sdk.currentuser.create()`) is * due to the email address already being in use. @@ -83,7 +90,7 @@ export const isPasswordRecoveryEmailNotVerifiedError = error => error && error.s * being closed or deleted. */ export const isTransactionInitiateListingNotFoundError = error => { - return responseErrors(error).some(apiError => { + return responseAPIErrors(error).some(apiError => { let notfound = false; try { @@ -105,7 +112,7 @@ export const isTransactionInitiateListingNotFoundError = error => { * due to the transaction total amount being too low for Stripe. */ export const isTransactionInitiateAmountTooLowError = error => { - return responseErrors(error).some(apiError => { + return responseAPIErrors(error).some(apiError => { const isPaymentFailedError = apiError.status === 402 && apiError.code === ERROR_CODE_PAYMENT_FAILED; let isAmountTooLow = false; @@ -134,3 +141,19 @@ export const isChangeEmailWrongPassword = error => error && error.status === 403 * is due to giving wrong password. */ export const isChangePasswordWrongPassword = error => error && error.status === 403; + +export const storableError = error => { + const { name, message, status, statusText } = error; + // Status, statusText, and data.errors are (possibly) added to the error object by SDK + const apiErrors = responseAPIErrors(error); + + // Returned object is the same as prop type check in util/proptypes -> error + return { + type: 'error', + name, + message, + status, + statusText, + apiErrors, + }; +};