Giter VIP home page Giter VIP logo

json-api-errors's Introduction

WTF-PHP - Json-Api-Errors Middleware

Json-Api-Errors provides two middlewares (PSR-7 or PSR-15 compliant), which format exceptions and returns JSON:API compliant errors.

You can either use the Middleware to return an error when it first occurs, or you can add them to an error bag to handle multiple errors (e.g. validation errors).

When handling multiple errors, the response status code will be the most general code determined from the bag.

For example:

  • 404 error and a 422 error, then 400 will be returned
  • 500 error and a 400 error, then 500 will be returned

Features

  • Json:API compliant
  • Use middleware that meets your needs:
    • PSR-7 compatible middleware
    • PSR-15 compatible middleware
  • Single or multiple exception handling
  • Steer the level of error detail via a debug flag

Usage

Examples

How to start the examples:

Proof of Concepts

There are two Proof of Concepts, that showcase the usage of the middlewares:

json-api-errors's People

Contributors

cathrinauer avatar fetzi avatar witti avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

witti

json-api-errors's Issues

Proof of Concept Slim4

  • Build a simple Slimv4 API with one Endpoint
  • Endpoint throws multiple Exceptions
  • Integrate Bag to handle those for a json:api compliant response

Handling Multiple Exceptions

Handling a Single Exception is already implemented. Now we also need to be able to handle multiple exceptions:

  • Middleware gets an empty bag in the Binding
  • Bag will be instantiated once (Singleton in Container)
  • Bag will be filled in Handler
  • Two ways:
    • try-catch for a Bag-Exception, for errors where one simply does not want to continue
    • Validation if bag has been filled when request is done for multiple possible exceptions during runtime

Remove JsonApiBody

Remove the JsonApiBody, if it is not necessary anymore.

Relates to Issue #28

Was done with Issue #35.

Create tests for JsonApiErrorMiddleware

To be able to implement the Middleware we need a set of test cases:

Cases for "normal" exceptions:

  • handle an Exception without a set error code and message
  • handle an Exception that defines a not supported HTTP status code
  • handle an Exception that defines a correct error code and message

Cases for json:api specific exceptions (JsonApiExceptionInterface):

  • handle a json:api Exception with status & title
  • handle a json:api Exception with status, code & title
  • handle a json:api Exception with status, code, title & detail
  • handle a json:api Exception with status, code, title, detail & source
  • handle a json:api Exception with status, code, title, detail, source & meta
  • handle a json:api Exception with status, code, title, detail, source, meta & ID

Missing:

  • Test cases for bag/list exceptions

Define Bag Interface

Define how a bag interface should look like.

What methods do we need?

  • push
  • add
  • remove (any need for this)
  • โ€ฆ

Proof of Concept Laravel

Build a simple Laravel API with one Endpoint
Endpoint throws multiple Exceptions
Integrate Bag to handle those for a json:api compliant response

Howto handle output details (debug flag)

Only setting a flag, which is false per default, the flag will be set via binding
Later on we can think about, using an array instead, with which it can be defined what should be set for e.g. Production

Debug-Flag false: Only Status, Code, Title
Debug-Flag true: Additionally Detail, Meta, Links

Rename ExceptionBag

  • Rename ExceptionBagInterface to ThrowablesBagInterface
  • Rename class ExcetionBag to ThrowablesBag

Create GitHub Actions

Do the setup for GitHub actions to

  • lint the src folder
  • test the source code by executing the PHPUnit tests
  • prevent force push to master
  • prevent merge before all reviews approved

Please make sure to add the status badges for the two actions to the readme.

Refactor Tests

  • Cleanup and rename json files
  • Simplify setting up tests with debug flag

Waits for Issue #28

Add Stacktrace to Meta

  • Move the Stacktrace info of an Exception to the meta-part
  • Only if debug-flag is set to true

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.