mejorar el reporte de errores

This commit is contained in:
f 2021-11-10 19:47:05 -03:00
parent 9a75dcadb7
commit cbbbd328ea

View file

@ -108,14 +108,14 @@ export class CartBaseController extends Controller {
async handleFailure (response) { async handleFailure (response) {
const data = { type: 'primary' } const data = { type: 'primary' }
let template = 'alert' let template = 'alert'
let notify = true
console.error(response.fail())
const site = window.site const site = window.site
const fail = response.fail()
switch (response.fail().name) { switch (fail.name) {
case 'MisconfigurationError': case 'MisconfigurationError':
data.content = response.fail().message data.content = fail.message
break break
case 'NoResponseError': case 'NoResponseError':
data.content = site.i18n.alerts.no_response_error data.content = site.i18n.alerts.no_response_error
@ -125,7 +125,7 @@ export class CartBaseController extends Controller {
break break
case 'BasicSpreeError': case 'BasicSpreeError':
// XXX: The order is missing, we need to start a new one // XXX: The order is missing, we need to start a new one
if (response.fail().serverResponse.status === 404) { if (fail.serverResponse.status === 404) {
template = 'recover_order' template = 'recover_order'
data.content = site.i18n.alerts.recover_order data.content = site.i18n.alerts.recover_order
} else { } else {
@ -134,15 +134,26 @@ export class CartBaseController extends Controller {
break break
case 'ExpandedSpreeError': case 'ExpandedSpreeError':
data.content = response.fail().summary const content = []
// XXX: La API devuelve los mensajes de error tal cual en la
// llave `error` pero el cliente solo nos da acceso a `errors`.
for (const field of Object.keys(fail.errors)) {
if (!site.i18n.errors?.fields[field]) {
console.error('El campo no tiene traducción', field)
}
content.push(`${site.i18n.errors?.fields[field]}: ${fail.errors[field].join(', ')}`)
}
data.content = content.join('. ')
notify = false
break break
default: default:
data.content = response.fail().message data.content = fail.message
} }
console.error(response.fail().name, data.content) if (notify) console.error(fail.name, data.content)
window.dispatchEvent(new CustomEvent('notification', { detail: { template, data } })) window.dispatchEvent(new CustomEvent('notification', { detail: { template, data } }))
} }