wakeonweb/errors-extra-library

There is no license information available for the latest version (v1.1.0) of this package.

Wakeonweb errors extra library

v1.1.0 2019-04-17 10:17 UTC

This package is auto-updated.

Last update: 2024-04-17 21:25:39 UTC


README

  • Error dictionary

Installation

composer.json

    "require": {
        "wakeonweb/errors-extra-library": "~1.0"
    }

AppKernel

$bundles[] = new WakeOnWeb\ErrorsExtraLibrary\App\Bundle\WakeonwebErrorsExtraLibraryBundle();

Configuration

Override http status code, show error messages on exceptions:

wakeonweb_errors_extra_library:
    force_format: json
    exception:
        http_status_codes:
            Pagerfanta\Exception\OutOfRangeCurrentPageException: 400
        show_messages:
            - Pagerfanta\Exception\OutOfRangeCurrentPageException
        log_levels:
            Pagerfanta\Exception\OutOfRangeCurrentPageException: notice
            Pagerfanta\Exception\NotValidCurrentPageException: error

Log level values as defined by PSR-3 (from RFC 5424).

Exception listener

The bundle adds an exception listener to format the response when the requested response format is application/json.

Default JSON response

  • code : HTTP status code
  • message : HTTP reason phrase

Example

{
    "code": 403,
    "message": "Forbidden",
}

Form Errors normalization

This bundle also standardize Symfony Form Errors.

With something like this:

    if (!$form->isValid()) {
        return new JsonResponse($this->normalizer->normalize($form, 'json'), 400);
    }

You will have:

{  
   "code":"400",
   "message":"Validation Failed",
   "errors":{  
      "children":{  
         "firstName":{  
            "errors":[  
               [  
                  "This value is too short. It should have {{ limit }} character or more.|This value is too short. It should have {{ limit }} characters or more.",
                  {  
                     "{{ value }}":"\"LL\"",
                     "{{ limit }}":3
                  }
               ]
            ]
         },
         "lastName":{  
            "errors":[  
               [  
                  "This value should not be blank.",
                  {  
                     "{{ value }}":"null"
                  }
               ]
            ]
         }
      },
      "errors":[  
         [  
            "This is a global form error with {{ param }}",
            {  
               "{{ param }}":"TEST"
            }
         ]
      ]
   }
}