UserError.ts 993 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import type { UserErrors, UserFieldError } from '#shared/types/error.ts'
  3. export default class UserError extends Error {
  4. public errors: UserErrors
  5. public generalErrors: ReadonlyArray<string>
  6. public fieldErrors: ReadonlyArray<UserFieldError>
  7. constructor(errors: UserErrors) {
  8. super()
  9. this.errors = errors
  10. this.generalErrors = errors
  11. .filter((error) => !error.field)
  12. .map((error) => error.message)
  13. this.fieldErrors = errors.filter(
  14. (error) => error.field,
  15. ) as ReadonlyArray<UserFieldError>
  16. // Set the prototype explicitly.
  17. Object.setPrototypeOf(this, new.target.prototype)
  18. }
  19. public getFieldErrorList(): Record<string, string> {
  20. return this.fieldErrors.reduce(
  21. (fieldErrorList: Record<string, string>, fieldError) => {
  22. fieldErrorList[fieldError.field] = fieldError.message
  23. return fieldErrorList
  24. },
  25. {},
  26. )
  27. }
  28. }