0

We have the following format of response to client if request is successful:

{
  "ExitCode": 1,
  "ErrorMessage": "",
  "NumberOfGreenBoxMatches": 4,
  "NumberOfPinkBoxExtractFrames": 5,
  "ProcessingTime": 4000,
  “HasOverlay”: false;
  "MatchResult": "...."
}

If a client request is bad or internal error is happened customer proposes to return the following:

{
  "ExitCode": -10,
  "ErrorMessage": "Internal error parsing pink boxes. Please contact tech support",
  "NumberOfGreenBoxMatches": 0,
  "NumberOfPinkBoxExtractFrames": 0,
  "ProcessingTime": 1240,
  “HasOverlay”: false;
  "MatchResult": ""
}

is it correct approach (return the similar json)? My opinion is no, we should return only like

{ "ErrorMessage": "Internal error parsing pink boxes. Please contact tech support" }

and correct HttpCode. What is correct way?

2 Answers 2

2

Your suggestion about correct approach looks more HTTP-standart compatible. Because ExitCode looks like HTTP-response codes functionality duplication.

But final solution i think is highly depends on client architecture. There is some questions that you must take into account:

  • Does client parse HTTP codes?
  • How client validates server responses?
  • Does server error-responses MUST be JSON-schema compatible with regular responses?
  • Does your client must always receive values in fields like ProcessingTime etc.
Sign up to request clarification or add additional context in comments.

5 Comments

How implements multilanguage in such way?
@OlegSh you should implement Accept-Language: header on client side. You can use that header in request for multilangual error messages.
ok, with multilanguage I agree. But there are many situations when need to have an additional 'exitcode'. I.e. user did not select an image and application should show special form for it...
@OlegSh Question case describes situation with one server-side fatal error. If you need to inform client about multiple errors (like multi-field form validation errors) just use errors field with errors array.
I don't mean multiple error. I mean, so, error can be "Invalid something" and "File is not uploaded" (both are bad request). In the first case we want to display a message with error, in the second - show File Upload dialog. So, we have to separate what is the problem on client part. We can do it using additional error code, because we can't do it by error message...
-1

The result from Web API is received using a Model, Here the Model used in Client side is

public class MyResult
{
    public int ExitCode { get; set; }
    public string ErrorMessage { get; set; }
    public int NumberOfGreenBoxMatches { get; set; }
    public int NumberOfPinkBoxExtractFrames { get; set; }
    public int ProcessingTime { get; set; }
    public bool HasOverlay { get; set; }
    public string MatchResult { get; set; }
}

if you send a different result upon error, which has single property "ErrorMessage", They cannot process it on client side.

so you better provide the information as per the exact model proposed by your customer.

5 Comments

status code says client that he should not process it as standard response
They can manage response based on information in it, for ex: As you pointed in Q, Exit Code : -10 . They can process the result based on this and display the error message.
they can, but HTTP requires return correct status code
then include one more property in the MyResult modal, public string StatusCode {get;set;} . Add the necessary data for each request. restapitutorial.com/httpstatuscodes.html
use try catch block, in catch use the MyResult modal as return, include error details with status code. but if your API is not reachable, your customer cannot expect the result like MyResult modal. Here they can use a validation like whether your API reachable before calling it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.