(error, each = false)
| 482 | * @returns {object|Array} Message props of the error object, or generic error object. |
| 483 | */ |
| 484 | export const toMessageProps = (error, each = false) => { |
| 485 | const props = [] |
| 486 | |
| 487 | // Check if it is an error message and transform it to a intl message. |
| 488 | if (isBackendErrorDetails(error) || isBackend(error)) { |
| 489 | const pathErrors = getBackendErrorDetailsPathError(error) |
| 490 | let errorDetails |
| 491 | |
| 492 | if ( |
| 493 | (hasValidDetails(error) && isBackendErrorDetails(error.details[0])) || |
| 494 | isBackendErrorDetails(error) |
| 495 | ) { |
| 496 | if (isBackendErrorDetails(pathErrors)) { |
| 497 | errorDetails = pathErrors |
| 498 | } else if ('details' in error && isBackendErrorDetails(error.details[0])) { |
| 499 | errorDetails = error.details[0] |
| 500 | } else { |
| 501 | errorDetails = error |
| 502 | } |
| 503 | |
| 504 | if (hasCauses(errorDetails)) { |
| 505 | // Use the root cause if any. |
| 506 | const rootCause = getBackendErrorRootCause(errorDetails) |
| 507 | |
| 508 | // If the attributes are missing values, use the generated default values based on the message. |
| 509 | const messageValues = rootCause?.message_format?.match(/[^{}]+(?=})/g) || [] |
| 510 | const values = messageValues.reduce( |
| 511 | (obj, val) => { |
| 512 | if (!(val in obj)) { |
| 513 | obj[val] = `{${val}}` |
| 514 | } |
| 515 | |
| 516 | return obj |
| 517 | }, |
| 518 | { ...rootCause.attributes }, |
| 519 | ) |
| 520 | |
| 521 | props.push({ |
| 522 | content: { |
| 523 | id: getBackendErrorDetailsId(rootCause), |
| 524 | defaultMessage: rootCause.message_format, |
| 525 | }, |
| 526 | values, |
| 527 | }) |
| 528 | } |
| 529 | |
| 530 | props.push({ |
| 531 | content: { |
| 532 | id: getBackendErrorDetailsId(errorDetails), |
| 533 | defaultMessage: errorDetails.message_format, |
| 534 | }, |
| 535 | values: errorDetails.attributes, |
| 536 | }) |
| 537 | } else { |
| 538 | props.push({ |
| 539 | content: { |
| 540 | id: getBackendErrorId(error), |
| 541 | defaultMessage: getBackendErrorDefaultMessage(error), |
no test coverage detected