Hi List,
I found myself a lot trying to elegantly handle EO creation errors when using ERRest as backend for (mostly) iOS applications using JSON.
For example, imagine i have an entity called User and for the user the emailAddress field is unique in the DB, so I won't be able to create two User objects with the same emailAddress.
I will have a UserController and inside that controller a createAction method... If I just put the simplest implementation possible like that:
public WOActionResults createAction() { User user = create(createFilter()); editingContext().saveChanges(); return response(user, showIDAndTokenFilter()); }
In case of error the user will get a 500 response with Content-Type: application/json but the response body won't be JSON but plain text of the exception (exposing all the details of the SQL query which is of course a security issue). That is a problem for example in iOS in which i get a parsing error as the json can't be parsed.
The way in which I usually approach the issue is of course to always put the saveChanges in a try-catch and then return errorResponse(ERXHttpStatusCodes.SOME_ERROR_CODE); and I use specific error codes for each of the possible cases (missing fields, duplicate entries, etc). So basically I use 4xx errors for most of the cases I catch and then fallback to 500 when something else happens.
I'm not sure this is the best approach as I'm pretty sure it's not standard so I'm asking for the wisdom of the community to try to come up with at least the ERRest standard. I considered returning a proper JSON response with a status code of 500 and some error message in a JSON Object, would it make sense?
Thanks,
Matteo
|