mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-01-30 19:52:20 +01:00
API error handling
This commit is contained in:
parent
8e8fc9b682
commit
66c205d0ed
@ -7,6 +7,39 @@ const client: AxiosInstance = axios.create({
|
|||||||
'X-CSRF-Token': token?.content || 'no-csrf-token'
|
'X-CSRF-Token': token?.content || 'no-csrf-token'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
|
client.interceptors.response.use(function (response) {
|
||||||
|
// Any status code that lie within the range of 2xx cause this function to trigger
|
||||||
|
return response;
|
||||||
|
}, function (error) {
|
||||||
|
// Any status codes that falls outside the range of 2xx cause this function to trigger
|
||||||
|
const message = error.response?.data || error.message || error;
|
||||||
|
return Promise.reject(extractHumanReadableMessage(message));
|
||||||
|
});
|
||||||
|
|
||||||
|
function extractHumanReadableMessage(error: any): string {
|
||||||
|
if (typeof error === 'string') return error;
|
||||||
|
|
||||||
|
let message = '';
|
||||||
|
if (error instanceof Object) {
|
||||||
|
// iterate through all the keys to build the message
|
||||||
|
for (const key in error) {
|
||||||
|
if (Object.prototype.hasOwnProperty.call(error, key)) {
|
||||||
|
message += `${key} : `;
|
||||||
|
if (error[key] instanceof Array) {
|
||||||
|
// standard rails messages are stored as {field: [error1, error2]}
|
||||||
|
// we rebuild them as "field: error1, error2"
|
||||||
|
message += error[key].join(', ');
|
||||||
|
} else {
|
||||||
|
message += error[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
return JSON.stringify(error);
|
||||||
|
}
|
||||||
|
|
||||||
export default client;
|
export default client;
|
||||||
|
@ -45,25 +45,31 @@ export const StripeForm: React.FC<StripeFormProps> = ({ onSubmit, onSuccess, onE
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
|
// stripe error
|
||||||
onError(error.message);
|
onError(error.message);
|
||||||
} else {
|
} else {
|
||||||
if (!paymentSchedule) {
|
try {
|
||||||
// process the normal payment pipeline, including SCA validation
|
if (!paymentSchedule) {
|
||||||
const res = await PaymentAPI.confirm(paymentMethod.id, cartItems);
|
// process the normal payment pipeline, including SCA validation
|
||||||
await handleServerConfirmation(res);
|
const res = await PaymentAPI.confirm(paymentMethod.id, cartItems);
|
||||||
} else {
|
await handleServerConfirmation(res);
|
||||||
// we start by associating the payment method with the user
|
|
||||||
const { client_secret } = await PaymentAPI.setupIntent(customer.id);
|
|
||||||
const { setupIntent, error } = await stripe.confirmCardSetup(client_secret, {
|
|
||||||
payment_method: paymentMethod.id
|
|
||||||
})
|
|
||||||
if (error) {
|
|
||||||
onError(error.message);
|
|
||||||
} else {
|
} else {
|
||||||
// then we confirm the payment schedule
|
// we start by associating the payment method with the user
|
||||||
const res = await PaymentAPI.confirmPaymentSchedule(setupIntent.id, cartItems);
|
const { client_secret } = await PaymentAPI.setupIntent(customer.id);
|
||||||
onSuccess(res);
|
const { setupIntent, error } = await stripe.confirmCardSetup(client_secret, {
|
||||||
|
payment_method: paymentMethod.id
|
||||||
|
})
|
||||||
|
if (error) {
|
||||||
|
onError(error.message);
|
||||||
|
} else {
|
||||||
|
// then we confirm the payment schedule
|
||||||
|
const res = await PaymentAPI.confirmPaymentSchedule(setupIntent.id, cartItems);
|
||||||
|
onSuccess(res);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} catch (err) {
|
||||||
|
// catch api errors
|
||||||
|
onError(err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,14 +11,12 @@
|
|||||||
position: relative;
|
position: relative;
|
||||||
margin-bottom: 0.75em;
|
margin-bottom: 0.75em;
|
||||||
.schedule-item-info {
|
.schedule-item-info {
|
||||||
display: block;
|
display: inline;
|
||||||
border-bottom: 1px solid #ddd;
|
border-bottom: 1px solid #ddd;
|
||||||
margin-right: 2em;
|
|
||||||
}
|
}
|
||||||
.schedule-item-price {
|
.schedule-item-price {
|
||||||
position: absolute;
|
display: inline;
|
||||||
top: 0;
|
border-bottom: 1px solid #ddd;
|
||||||
right: 2em;
|
|
||||||
}
|
}
|
||||||
.schedule-item-date {
|
.schedule-item-date {
|
||||||
display: block;
|
display: block;
|
||||||
|
@ -10,7 +10,6 @@
|
|||||||
padding: 15px;
|
padding: 15px;
|
||||||
|
|
||||||
.stripe-errors {
|
.stripe-errors {
|
||||||
height: 20px;
|
|
||||||
padding: 4px 0;
|
padding: 4px 0;
|
||||||
color: #9e2146;
|
color: #9e2146;
|
||||||
margin-bottom: 1.2em;
|
margin-bottom: 1.2em;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user