contributte / apitte

:wrench: An opinionated and enjoyable API framework based on Nette Framework. Supporting content negotiation, debugging, middlewares, attributes, annotations and loving openapi/swagger.
https://contributte.org/packages/contributte/apitte/
MIT License
61 stars 36 forks source link

Output all error messages at once #59

Closed mabar closed 4 years ago

mabar commented 6 years ago

Currently once a client error is found then it's immediatelly written to output. We should e.g. validate all request parameters at once then output errors. Validate whole request entity (and all request entities once #46 is implemented) then output errors. Endpoints should behave the same.

Probably best would be an configurable exception, like $clientErrorException->addError($error).

f3l1x commented 6 years ago

Looks 👍

f3l1x commented 6 years ago

Lets put it on v0.5. OK?

mabar commented 5 years ago

Thinked about it a lot and I have new proposal.

$exception = new ClientErrorException('', 404);
$exception->withMessages(['foo', 'bar', 'baz']);

Output would be (for default ErrorHandler and Json(Unify)Transformer

[
  'status' => 'error',
  'code' => 404,
  'errors' => ['foo', 'bar', 'baz'],
  'context' => ['...'],
]

I deliberately don't use data key as we use currently for errors. Data will don't have different format in case of error and also it's not really needed to return only errors if an error occured.

We could in future create mechanism to add errors without throwing exception so api could work with valid and invalid data at the same time. In current state if one of requested entities (e.g. multiple orders) failed to be processed then whole request fails.

mabar commented 5 years ago

Also add:

mabar commented 5 years ago

We should also add an optional reason to errors. Oftenly error 400 could be created for many reasons and parsing error message is no-way