aqueduct icon indicating copy to clipboard operation
aqueduct copied to clipboard

Evaluate generic result container

Open itsjoeconway opened this issue 10 years ago • 6 comments

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};
  }
}

itsjoeconway avatar Feb 06 '16 02:02 itsjoeconway

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);
} 

itsjoeconway avatar Feb 06 '16 02:02 itsjoeconway

Would like feedback from @anachlas @rosshambrick @JesseBlack82

itsjoeconway avatar Feb 06 '16 02:02 itsjoeconway

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.

anachlas avatar Feb 06 '16 22:02 anachlas

In the current behavior, does this level of failure return a 500? What is the motivation for the proposed change?

JesseBlack82 avatar Feb 09 '16 21:02 JesseBlack82

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.

itsjoeconway avatar Feb 09 '16 22:02 itsjoeconway

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.

rosshambrick avatar Feb 10 '16 15:02 rosshambrick