File size: 1,103 Bytes
83a59ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import Bag from './bag'
import { capitalize } from './capitalize'
import { toApiError } from './api-error'

export class ErrorMessages extends Bag<string> {}

const sanitize = (key: string, values: Array<string>): Array<string> => {
  return values.map((value: string) => {
    return capitalize(
      value
        .split(key)
        .join(key.replace('_', ' '))
        .split('_')
        .join(' ')
        .split('-')
        .join(' ')
        .split(' char')
        .join(' character')
        .split(' field ')
        .join(' '),
    )
  })
}

export const getErrorMessages = (error: unknown): ErrorMessages => {
  const errors = new ErrorMessages()
  const apiError = toApiError(error)
  const response = 'response' in apiError ? toApiError(apiError.response) : {}
  const data = apiError.data?.data ?? response.data?.data
  const status = apiError.status ?? response.status
  if (typeof data !== 'object' || data === null || status !== 422) {
    return errors
  }

  Object.entries(data).forEach(([key, values]) => {
    errors.addMany(key, sanitize(key, values))
  })

  return errors
}