wakeonweb/errors-extra-library

该软件包的最新版本(v1.1.0)没有提供许可证信息。

Wakeonweb 错误额外库

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

This package is auto-updated.

Last update: 2024-09-17 22:36:56 UTC


README

  • 错误字典

安装

composer.json

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

AppKernel

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

配置

覆盖 http 状态码,在异常时显示错误消息

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

日志级别值,由 PSR-3 定义(来自 RFC 5424)。

异常监听器

该组件添加了一个异常监听器,以便在请求的响应格式为 application/json 时格式化响应。

默认 JSON 响应

  • code : HTTP 状态码
  • message : HTTP 原因短语

示例

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

表单错误标准化

此组件还将标准化 Symfony 表单错误。

类似以下内容

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

您将得到

{  
   "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"
            }
         ]
      ]
   }
}