ReasonPhrase is always empty

Viewed 59

The rest API server is under "Spring boot", the client is under XAMARIN FORM and I use HttpClient to get or post data to the server.

Is there a specific format that must be sent by a rest API in case of an error to fill, in HttpClient, the ReasonPhrase? Because the ReasonPhrase is always empty even on error.

the format I send from the rest API is normally a standard like this :

{
    "timestamp": "2022-09-11T17:58:02.811+00:00",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
}

In the example, the reasonPhrase should have "No message available"

SPRING BOOT code

My Exception Handler code:

@ControllerAdvice
public class AppExceptionsHandler extends ResponseEntityExceptionHandler {

    // Handle a specific error
    @ExceptionHandler(value = { ApplicationException.class })
    public ResponseEntity<Object> handleUserServiceException(ApplicationException ex, WebRequest request) {
        ErrorMessage errorMessage = new ErrorMessage(new Date(), HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());

        return new ResponseEntity<>(errorMessage, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    // Handle unknown error
    @ExceptionHandler(value = { Exception.class })
    public ResponseEntity<Object> handleOtherExceptions(Exception ex, WebRequest request) {
        ErrorMessage errorMessage = new ErrorMessage(new Date(), ex.getMessage());

        return new ResponseEntity<>(errorMessage, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

My Exception code:

public class ApplicationException extends RuntimeException{

    private static final long serialVersionUID = 7289264700624277238L;
    
    public ApplicationException(String message)
    {
        super(message);
    }

}

My error message object code:

public class ErrorMessage {
    private Date timestamp;
    private HttpStatus status;
    private String message;
    
    public ErrorMessage() {}
    
    public ErrorMessage(Date timestamp, HttpStatus status, String message)
    {
        this();
        this.timestamp = timestamp;
        this.message = message;
        this.status = status;
    }
     *** some more code getter and setter ***
}

When I throw the exception in the code:

    *** some code ***
throw new ApplicationException("Wrong user");

Xamarin code

internal class HttpMethods
{
    private HttpClient _httpClient;

    public HttpMethods()
    {
        _httpClient = new HttpClient();
        _httpClient.Timeout = TimeSpan.FromSeconds(15);
        _httpClient.MaxResponseContentBufferSize = 256000;
        _httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
    }

    public async Task<(TA, ErrorDto)> HttpPost<TA, TB>(string url, string token, TB data)
    {
        var result = default(TA);
        var error = new ErrorDto();
        HttpResponseMessage response = new();

        _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

        HttpContent content;
        content = FileContent(data as string);

        response = await _httpClient.PostAsync(new Uri(url), content);

        if (response.IsSuccessStatusCode)
        {
            var jsonDataResponse = await response.Content.ReadAsStringAsync();
            result = JsonConvert.DeserializeObject<TA>(jsonDataResponse);
        }
        else
        {
            error.ReasonPhrase = (int)response.StatusCode;
            error.StatusCode = (string)response.ReasonPhrase;
        }
                
        return (result, error);
    }
}

Thanks for your help

PS: Edited for code details.

0 Answers
Related