Difficulty Mapping FusionAuth Errors to TypeScript due to changing nested object names

57 views Asked by At

I am trying to map FusionAuth errors into Typescript interfaces for improved error handling. But I am running into problems.

An example error returned is the following nested object:

{"statusCode":400,"exception":{"fieldErrors":{"email":[{"code":"[duplicate]email","message":"A User with email already exists."}]}}}

And another error returned by FusionAuth is the following:

{"statusCode":400,"exception":{"fieldErrors":{"password":[{"code":"[tooShort]password","message":"The [user.password] property is shorter than the minimum of [8] characters."}]}}}

As you can see, exception.fieldErrors.XYZ is a different object name for each error. I want to try and abstract the exception.fieldErrors.XYZ error into typescript classes, and create a method that iterates over the exception.fieldErrors.XYZ object, and pushes them into an array. Below are some TypeScript classes at an attempt of solving this.

export class ClientResponseError {
  statusCode: string;
  exception: Exception;
}


export interface ExceptionName {
  message: string;
}

export interface FieldErrors {
  fieldError: ExceptionName[];
}

export interface Exception {
  fieldErrors: FieldErrors;
}

and the following short snippet to test printing out the messages

error.exception.fieldErrors.fieldError.map(error => {
     console.log(error.message)
    })

The main error returned is the following TypeError: Cannot read properties of undefined (reading 'map') Any suggestions for dealing with this? The problem lies that exception.fieldErrors.XYZ is a different object name for each error, and I want to catch all errors. Thanks in advance

0

There are 0 answers