diff --git a/packages/legacy-compat/src/legacy-network-handler/legacy-network-handler.ts b/packages/legacy-compat/src/legacy-network-handler/legacy-network-handler.ts index 9df016ecd76..3d0f0ebfe3a 100644 --- a/packages/legacy-compat/src/legacy-network-handler/legacy-network-handler.ts +++ b/packages/legacy-compat/src/legacy-network-handler/legacy-network-handler.ts @@ -231,7 +231,7 @@ function adapterDidInvalidate( if (serializer && typeof serializer.extractErrors === 'function') { let errorsHash = serializer.extractErrors(store, store.modelFor(identifier.type), error, identifier.id) as Record< string, - string | string[] + string | string[] | JsonApiError[] >; error.errors = errorsHashToArray(errorsHash); } @@ -256,31 +256,40 @@ function adapterDidInvalidate( } } -function makeArray(value: T | T[]): T[] { - return Array.isArray(value) ? value : [value]; -} - const PRIMARY_ATTRIBUTE_KEY = 'base'; -function errorsHashToArray(errors: Record): JsonApiError[] { +function errorsHashToArray(errorsHash: Record): JsonApiError[] { const out: JsonApiError[] = []; - if (errors) { - Object.keys(errors).forEach((key) => { - let messages = makeArray(errors[key]); - for (let i = 0; i < messages.length; i++) { + if (errorsHash) { + Object.keys(errorsHash).forEach((key) => { + let errorsItem = errorsHash[key] + let errors = typeof errorsItem === 'string' ? [errorsItem] : errorsItem; + for (let i = 0; i < errors.length; i++) { let title = 'Invalid Attribute'; let pointer = `/data/attributes/${key}`; if (key === PRIMARY_ATTRIBUTE_KEY) { title = 'Invalid Document'; pointer = `/data`; } - out.push({ - title: title, - detail: messages[i], - source: { - pointer: pointer, - }, - }); + let error = errors[i] + if (typeof error === 'string') { + out.push({ + title: title, + detail: error, + source: { + pointer: pointer, + }, + }); + } else { + out.push({ + ...error, + title: title, + detail: error.detail || error.title, + source: { + pointer: pointer, + }, + }); + } } }); } diff --git a/packages/serializer/src/json.js b/packages/serializer/src/json.js index 04407088653..bcc075a746d 100644 --- a/packages/serializer/src/json.js +++ b/packages/serializer/src/json.js @@ -1476,19 +1476,25 @@ const JSONSerializer = Serializer.extend({ const extracted = {}; payload.errors.forEach((error) => { - if (error.source && error.source.pointer) { - let key = error.source.pointer.match(SOURCE_POINTER_REGEXP); - - if (key) { - key = key[2]; - } else if (error.source.pointer.search(SOURCE_POINTER_PRIMARY_REGEXP) !== -1) { - key = PRIMARY_ATTRIBUTE_KEY; - } - - if (key) { - extracted[key] = extracted[key] || []; - extracted[key].push(error.detail || error.title); - } + const errorPointer = error.source?.pointer ?? '/data' + let key = errorPointer.match(SOURCE_POINTER_REGEXP); + if (key) { + key = key[2]; + } else if (errorPointer.search(SOURCE_POINTER_PRIMARY_REGEXP) !== -1) { + key = PRIMARY_ATTRIBUTE_KEY; + } + if (key) { + extracted[key] = extracted[key] || []; + // Keep only what are valid attr's for a jsonapi error + extracted[key].push({ + id: error.id, + links: error.links ? { about: error.links.about, type: error.links.type } : undefined, + status: error.status, + code: error.code, + detail: error.detail, + title: error.title, + meta: error.meta + }); } });