24
Views
3
Comments
Exposed Rest API and handling errors with OR without logging
Question

Hi All. 


I'm in sort of a dead end in error handling in an exposed rest API and wanted to know if any of yhou have a luminous idea. 


The situation:


We have a rest API we expose to a few other (non-outsystems) parts of our organisation. Some of them handle the statuscodes. We for instance like to throw a 403 if someone is not allowed to do something or a 412 when a precondition for a method is not met. But we also post back a body giving hints as to what is going wrong. We do this through an error-handling mechanism:


You see we actually repeat the rasing of the error, because that is the only way to let OutSystems paste the error in the body of the response. We however do NOT always want to see the error in the log. 


If someone has an InputDataException (we call it that, also serves as a NotFound message) i would like my service to return this (i simplified the response a bit):

{

    "Errors": [

        {

            "The ..... you are looking for is not found. You where looking for .... With this number: 000000 ", (the number is a reflection of the input)

        }

    ],

    "StatusCode": 404

}



AND when someone is not allowed to do this:


{

    "Errors": [

        {

            "You are not allowed to execute this action"

        }

    ],

    "StatusCode": 403

}


In the first case i do NOT want to see the error in the log, in the second case i DO want to see it in the log. 


The only way i see this being possible is to add a flag (more of a complete struct) to the output structure of every method and catching that in the on response. But i really do not like putting stuff like that in my rest api definition. It is used for setting a status code so it shouldn't be in the response. But the only way to get the response body to be different is by throwing an error. ANd those get logged...


Anyone an idea?

Rank: #129

Hi Alexander,


If I understand you correctly, you want to control by type of exception if they will generate a log in Service Center, right?

You can do that on the exception handler via the property "Log Error" like on the image below:



I hope it helps.

João

Rank: #4785

Hi João,


Thanks for your answer. This is indeed what i want. Control over what will be logged. However: I do want the output of the API to remain the same as when the error is uncaught. An error struct just like the error Outsystems pastes in the body if an error is raised (and uncaught). 


This way my API has two possibles responses:

1) Normal json struct with normal respons (200 OK)

2) Error json struct with errors (text explanation and accompanying http status code)


This way i can use the raise error method to handle every situation i do not aprove of (input not correct, record not found, other errors etc....) through the outsystems method of handling errors. I am however not very interested in logging each error. And the API will only output what is in the output structure UNLESS you raise an error (and not catch it). For now i just include the entire error struct in each output structure. But this is sort of sloppy work. I'd rather be able to tell other developers (non-outsystems) this is my swagger for the normal responses, and an error will always look like this.....

Kind regards

Alexander

Rank: #129

Hi Alexander,


I think if you define the structure record as not mandatory, it won't be included in the output when you don't fill anything. For example:


In this example, the structure Example would be your REST output. If ou don't fill anything on the Error structure, it won't appear in the output.


Cheers,

João