loadsys / cakephp-serializers-errors
用于序列化 CakePHP 错误和异常,主要用于 HTML、JSON 或 JSON API 格式。
1.0.2
2016-03-21 02:55 UTC
Requires
- php: >=5.4.0
- ext-json: *
- composer/installers: ~1.0
Suggests
- loadsys/cakephp_serializers: Used for serializing CakePHP data into API style formats.
This package is not auto-updated.
Last update: 2024-09-11 16:12:48 UTC
README
用于序列化 CakePHP 错误和异常,主要用于 HTML、JSON 或 JSON API 格式。
添加两个新的异常类,用于扩展以获取 JSON API 格式的错误消息。
要求
- CakePHP 2.3+
- PHP 5.4+
安装
Composer
$ composer require loadsys/cakephp-serializers-errors:~1.0
用法
- 通过在您的 bootstrap.php 中添加此行将此插件添加到您的应用程序
CakePlugin::load('SerializersErrors', array('bootstrap' => true));
- 更新您的
core.php
以使用插件的 ExceptionRenderer 替代核心的
Configure::write('Exception', array( 'handler' => 'ErrorHandler::handleException', 'renderer' => 'SerializersErrors.SerializerExceptionRenderer', 'log' => true, ));
- 完成此操作后,异常将作为可能的内容渲染,JSON API 错误、JSON 格式错误或标准 HTML 响应,具体取决于请求的
Accepts
标头。 - 因此,如果您使用
Accepts: application/vnd.api+json
则返回 JSON API 错误Accepts: application/json
则返回 JSON 错误Accepts: */*
则返回正常的 CakePHP HTML 风格错误
- 如果您构建了扩展
BaseSerializerException
的自定义异常,您将获得能够启用 JSON API 错误 完整功能集的异常,同时在上述模式中渲染。
示例响应
以下是不同异常类的示例响应。
BaseSerializerException
Accepts: application/vnd.api+json
符合 JSON API 预期格式
throw new BaseSerializerException("This is a message.", "Something failed", 400, "Custom ID For Error", "http://docs.domain.com/api/v1/custom-id-for-error", array(), array())
{ "errors": [ { "id": "Custom ID For Error", "href": "http://docs.domain.com/api/v1/custom-id-for-error", "status": "401", "code": "401", "title": "Title of the Error", "detail": "More Detailed information", "links": [], "paths": [] } ] }
Accepts: application/json
throw new BaseSerializerException("This is a message.", "Something failed", 400, "Custom ID For Error", "http://docs.domain.com/api/v1/custom-id-for-error", array(), array())
{ "id": "Custom ID For Error", "href": "http://docs.domain.com/api/v1/custom-id-for-error", "status": "400", "code": "400", "detail": "Something failed", "links": [], "paths": [] }
ValidationBaseSerializerException
Accepts: application/vnd.api+json
符合 JSON API 预期格式
throw new ValidationBaseSerializerException("This is a message.", $this->ModelName->invalidFields(), 422, "Custom ID For Error", "http://docs.domain.com/api/v1/custom-id-for-error", array(), array())
{ "errors": { "id": "Custom ID For Error", "href": "http://docs.domain.com/api/v1/custom-id-for-error", "status": "400", "code": "400", "title": "This is a message.", "detail": { "username": [ "Username can not be empty", "Username can only be alphanumeric characters" ], "first_name": [ "First Name must be longer than 4 characters" ] }, "links": [], "paths": [] } }
Accepts: application/json
符合 Ember.js DS.Errors 类 预期格式
$invalidFields = $this->ModelName->invalidFields(); throw new ValidationBaseSerializerException("This is a message.", $invalidFields, 422, "Custom ID For Error", "http://docs.domain.com/api/v1/custom-id-for-error", array(), array())
{ "errors": { "name": [ "Name must not be empty.", "Name must be only alphanumeric characters" ], "status": [ "Status? must be true or false." ], "SubModel": [ { "options": [ "Options must take the form `first|second|third` and `formula` must be empty." ] } ] } }
贡献
报告问题
请使用 GitHub Issues 列出任何已知的缺陷或问题。
开发
在开发此插件时,请进行分支并提交 PR 以进行任何新开发。
可以通过此命令运行插件的完整测试套件
./lib/Cake/Console/cake test SerializersErrors AllSerializersErrors