Skip to content

Commit

Permalink
justeat#330: Adding new ErrorWithMessageResponse object to handle err…
Browse files Browse the repository at this point in the history
…or with object body

We received two different error message formats.

one:
{
    "error": "Couldn't authenticate you"
}

two:
{
    "error": {
        "title": "Forbidden",
        "message": "You do not have access to this page. Please contact the account owner of this help desk for further help."
    }
}

The current ErrorResponse model class was able to handle the first response but would throw this error with the second response: "Unexpected character encountered while parsing value: {. Path 'error', line 2, position 12" because it's expecting a string value for error but instead it hits a complex object . The new ErrorWithMessageResponse model class will allow the user to deserialize the second response and convert it to an ErrorResponse object.
  • Loading branch information
MatthewBAllen committed Aug 30, 2024
1 parent 969bf09 commit ac59329
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
38 changes: 38 additions & 0 deletions src/ZendeskApi.Client/Exceptions/ErrorWithMessageResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace ZendeskApi.Client.Exceptions
{
/// <summary />
public class ErrorWithMessageResponse
{
/// <summary />
public ErrorWithMessageResponse()
{
}

/// <summary />
[JsonProperty("error")]
public ErrorWithMessage Error { get; internal set; }

/// <summary />
public ErrorResponse ToErrorResponse(JObject details = null) => new ErrorResponse()
{
Error = Error?.Title,
Description = Error?.Message,
Details = details
};

/// <summary />
public class ErrorWithMessage()
{
/// <summary />
[JsonProperty("message")]
public string Message { get; internal set; }

/// <summary />
[JsonProperty("title")]
public string Title { get; internal set; }
}
}
}
33 changes: 30 additions & 3 deletions src/ZendeskApi.Client/Exceptions/ZendeskRequestExceptionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,39 @@ public async Task<ZendeskRequestException> Build()
(int)_response.StatusCode <= 499 &&
!_doNotBuildErrorModelResponseCodes.Contains((int)_response.StatusCode))
{
error = await _response.Content.ReadAsAsync<ErrorResponse>();
var content = await _response.Content.ReadAsStringAsync();

try
{
error = JsonConvert.DeserializeObject<ErrorResponse>(content);
}
catch (JsonReaderException)
{
try
{
error = JsonConvert.DeserializeObject<ErrorWithMessageResponse>(content)?.ToErrorResponse();
}
catch
{
// Swallow a deserialization failure here.
}

// Throw the original exception if an error could not be deserialized.
if ( error == null )
throw;
}

if (error?.Error != null && error.Description != null)
{
var detail = Environment.NewLine + JsonConvert.SerializeObject(error.Details, Formatting.Indented);
message.AppendLine($"{error.Error}: {error.Description}. {detail}");
if (error.Details == null)
{
message.AppendLine($"{error.Error}: {error.Description.TrimEnd('.')}.");
}
else
{
var detail = Environment.NewLine + JsonConvert.SerializeObject(error.Details, Formatting.Indented);
message.AppendLine($"{error.Error}: {error.Description}. {detail}");
}
}
}

Expand Down

0 comments on commit ac59329

Please sign in to comment.