Throwing a custom C# .Net exception does not get recognized by Axios in React

Viewed 28

This is how I throw the exception:

throw RestExceptionHandler.ThrowException(ErrorCode.WebServiceConnectorUnableToGetUser);

This is the ThrowException method:

        public static RestServerException ThrowException(ErrorCode errorCode, object[] parameters = null, string property = null)
        {
            try
            {
                var value = ErrorCodes.errorList.First(code => code.errorCode == errorCode);

                if (parameters?.Length > 0)
                {
                    return new RestServerException(string.Format(value.description, parameters), 
                        value.httpStatusCode, new ErrorDescription
                        {
                            property = property ?? value.property,
                            errorCode = value.errorCode
                        });    
                }

                value.description = value.description.Replace("'{0}'", "");//removing placeholder {0} if there are no parameters given
                return new RestServerException(value.description, 
                    value.httpStatusCode, new ErrorDescription
                    {
                        property = property ?? value.property,
                        errorCode = value.errorCode
                    });

            }
            catch (Exception e)
            {
                Log.Error(e, "Error: {@Exception}", e);
                throw;
            }
        }

This is the exception error body:

error

Now comes the React code, this is the post function in my service:

  getUser(request: any) {
    const res = axios
      .post<any, AxiosResponse<any>>("https://localhost:5002/v/1/signumid_integrations/user", request)
      .then((res) => {
        console.log(res);
        return res.data;
      })
      .catch(function (err) {
        console.log(err);
      });
    return res;
  }

And this is how I call the getUser method:

        httpClient
          .getUser(request)
          .then((res2) => {
           
            initiator.current =
              res2.mail === null || res2.mail === undefined || res2.mail === "" ? res2.userPrincipalName : res2.mail;
          });

And this is the Axios error that I get every time. It is a default empty network error every time. axios

What could be the problem here and how do I solve it?

This is what the getUser method looks in network: It returns 500, even though i set it to 400.

getuser

This is the controller in minimal api code:

app.MapPost("v/1/signumid_integrations/user", async (ExchangeOBORequest request) =>
    await IntegrationsService.GetUser(request));
0 Answers
Related