Skip to content

Cannot deserialize custom error response from REST call to a RestResource #354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
zonkflut opened this issue Sep 23, 2019 · 3 comments
Open

Comments

@zonkflut
Copy link

I am having issues with the ForceClient.ExecuteRestApiAsync() methods.

I have written a custom RestResource in my salesforce instance similar to this:

@RestResource(urlMapping='/myResource/*')
global class MyResource {
  @HttpPost
  global static MyResponse post() {
    try {
      // Some operation that results in an exception
    }
    catch (Exception e) {
      RestContext.response.statusCode = 400;
      MyResponse r = new MyResponse();
      r.myCustomMessage = 'Something happened: ' + e.getMessage();
    }
  }
}

Then using the Force Client as below

SalesforceConnector.ForceClient.ExecuteRestApiAsync<MyResponse>("myResource");

after evaluating the async task I get the exception:

System.Exception : Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'Salesforce.Common.Models.Json.ErrorResponses' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'myCustomMessage ', line 1, position 19. ----> 

--JsonSerializationException
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Salesforce.Common.JsonHttpClient.ParseForceException(String responseMessage)
at Salesforce.Common.JsonHttpClient.<HttpPostAsync>d__9`1.MoveNext()

In the method Salesforce.Common.JsonHttpClient.ParseForceException(String responseMessage) I have found that the code attempts to deserialize any response that doesn't have a successful HTTP Response Code to an ErrorResponses model, which is a collection of ErrorResponse classes.

I understand that this is the default error model for DML requests through the Salesforce API, but it doesn't allow for handling error response implementations for RestResources. Are we able to resolve this?

@zonkflut
Copy link
Author

@wadewegner just wondering if there is anything I need to do to get movement on this?

@wadewegner
Copy link
Owner

@zonkflut Can you send a fix with tests as a pull request?

@zonkflut
Copy link
Author

Hey @wadewegner, I have submitted a pull request (link above), however the test automation in the CI/CD pipeline has old configuration and is failing tests. Are you able to let me know how to proceed.

Thanks,
Erik

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants