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) {
const data = { type: 'primary' }
let template = 'alert'
console.error(response.fail())
let notify = true
const site = window.site
const fail = response.fail()
switch (response.fail().name) {
switch (fail.name) {
case 'MisconfigurationError':
data.content = response.fail().message
data.content = fail.message
break
case 'NoResponseError':
data.content = site.i18n.alerts.no_response_error
@ -125,7 +125,7 @@ export class CartBaseController extends Controller {
break
case 'BasicSpreeError':
// 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'
data.content = site.i18n.alerts.recover_order
} else {
@ -134,15 +134,26 @@ export class CartBaseController extends Controller {
break
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
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 } }))
}