Evaluate generic result container
Currently, async operations (especially DB related stuff) throw exceptions on failure. Investigate whether returning a tuple-like structure is a better approach. It would require more hands-on approach within request handlers, but it'd be more explicit and less magical. Example result object:
class FailableOperationResult<T> implements Serializable {
T value;
dynamic error;
int statusCode;
String message;
bool get didFail {
return error != null;
}
dynamic asSerializable() {
return {"error" : message};
}
}
And then utilization would be:
@httpGet findThings() {
var q = new Query<Something>();
var result = await q.fetch();
if (result.didFail) {
return new Response.fromOperation(result);
}
return new Response.ok(result.value);
}
Would like feedback from @anachlas @rosshambrick @JesseBlack82
The biggest issue I see with it compared to throwing an exception is you would have to remember to check for success after every database call. I like that with the current approach there is a reasonable default behavior.
In the current behavior, does this level of failure return a 500? What is the motivation for the proposed change?
This level of failure goes thru a transformation - if monadart recognizes the error, it returns an appropriate status code. I.e., a database conflict is a 409. It defaults to 500. The motivation would be to make error handling more explicit at the cost of making it more automatic.
Having a good default behavior would make implementation simpler. But the ability to catch and do something different if needed is good to have.
I do worry that requiring an evaluation of the result to provide the proper response will result in the developer forgetting to do it. If the "compiler" required it, it would be another story.