mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-10-29 10:18:29 -04:00
15 lines
795 B
JavaScript
15 lines
795 B
JavaScript
// What to tell someone when saving from a dialog failed. A 422 has the server's
|
|
// own reasons, by field ("Quantity must be…", or just the reason when it isn't
|
|
// about a field); anything else gets a general message.
|
|
export function errorMessages(status, data, {forbidden, signedOut} = {}) {
|
|
if (status === 422 && data && data.errors) {
|
|
const messages = Object.entries(data.errors).flatMap(([field, list]) => (
|
|
list.map((message) => (field === 'base' ? message : `${field.replace(/_/g, ' ').replace(/^./, (c) => c.toUpperCase())} ${message}`))
|
|
));
|
|
if (messages.length > 0) return messages;
|
|
}
|
|
if (status === 403 && forbidden) return [forbidden];
|
|
if (status === 401 && signedOut) return [signedOut];
|
|
return ['Something went wrong saving that. Please try again.'];
|
|
}
|