antarctica / laravel-json-exception-formatter

Laravel JSON 异常格式化包的定制接口。

v0.1.4 2015-01-09 12:17 UTC

This package is not auto-updated.

Last update: 2024-09-24 03:14:46 UTC


README

Laravel JSON Exception Formatter 包的定制接口。

此包旨在与扩展 antarctica/laravel-base-exceptions 包的异常一起使用,但不需要此依赖。

安装

在您的 composer.json 文件中要求此包

{
	"require": {
		"antarctica/laravel-json-exception-formatter": "dev-develop"
	}
}

运行 composer update

app/config/app.php 中注册服务提供者

'providers' => array(

	'Antarctica\JsonExceptionFormatter\JsonExceptionFormatterServiceProvider',
	
),

注意:此包在幕后自动要求并注册了 JSON Exception Formatter 包。

使用方法

注册服务提供者就足够自动调用此包。底层的 JSON Exception Formatter 会在请求JSON响应(即 accept: application/json)并抛出异常时触发。

此包是底层包的 自定义格式化器

它为调试和非调试环境提供异常的错误表示。

我们自定义实现的主要区别是包含来自 HttpException 异常的 antarctica/laravel-base-exceptions 包的自定义属性。

扩展其他异常的异常将不会包含此额外信息,但不会删除任何信息(即此包是透明的)。

除了额外的错误信息外,调试环境将在响应中包含PHP堆栈跟踪,以帮助诊断。

示例用法

基于响应的异常

<?php

use Antarctica\LaravelBaseExceptions\Exception\HttpException;

class AuthenticationException extends HttpException {

    protected $statusCode = 401;

    protected $kind = 'authentication_failure';

    protected $details = [
        "authentication_error" => [
            "Ensure your credentials are correct and that your user account is still active, or contact the maintainer of this API for assistance."
        ]
    ];
}

非调试环境中的异常

{
    "errors": [
        {
            "details": {
                "authentication_error": [
                    "Ensure your credentials are correct and that your user account is still active, or contact the maintainer of this API for assistance."
                ]
            },
            "type": "authentication_failure"
        }
    ]
}

调试环境中的异常

{
    "errors": [
        {
            "details": {
                "authentication_error": [
                    "Ensure your credentials are correct and that your user account is still active, or contact the maintainer of this API for assistance."
                ]
            },
            "exception": "Antarctica\\LaravelTokenAuth\\Exception\\Auth\\AuthenticationException",
            "file": "/app/vendor/antarctica/laravel-token-auth/src/Antarctica/LaravelTokenAuth/Service/Token/TokenServiceJwtAuth.php",
            "kind": "authentication_failure",
            "line": 88,
            "stack_trace": [
                {
                    "file": "/app/vendor/antarctica/laravel-token-auth/src/Antarctica/LaravelTokenAuth/Service/TokenUser/TokenUserService.php",
                    "line": 91,
                    "function": "authOnce",
                    "class": "Antarctica\\LaravelTokenAuth\\Service\\Token\\TokenServiceJwtAuth",
                    "type": "->",
                    "args": [
                        {
                            "username": "xxx",
                            "password": "xxx"
                        }
                    ]
                },
                ...
                {
                    "file": "/app/public/index.php",
                    "line": 49,
                    "function": "run",
                    "class": "Illuminate\\Foundation\\Application",
                    "type": "->",
                    "args": []
                }
            ]
        }
    ]
}

贡献

本项目欢迎贡献,请参阅 CONTRIBUTING 了解我们的通用政策。

开发

为了帮助开发和保持本地计算机整洁,使用了一个由Vagrant管理的VM,以创建一个具有所有必要工具/库的隔离环境。

要求

  • Mac OS X
  • Ansible brew install ansible
  • VMware Fusion
  • Vagrant brew cask install vmware-fusion vagrant
  • 主机管理器Vagrant VMware 插件 vagrant plugin install vagrant-hostmanager && vagrant plugin install vagrant-vmware-fusion
  • 您在 ~/.ssh/ 中有一个名为 id_rsa 的私钥和名为 id_rsa.pub 的公钥
  • 您在 ~/.ssh/config 中有一个类似 [1] 的条目

SSH配置条目

Host bslweb-*
    ForwardAgent yes
    User app
    IdentityFile ~/.ssh/id_rsa
    Port 22

开发VM的配置

VM使用Vagrant管理,并通过Ansible配置。

$ git clone ssh://git@stash.ceh.ac.uk:7999/basweb/laravel-json-exception-formatter.git
$ cp ~/.ssh/id_rsa.pub laravel-json-exception-formatter/provisioning/public_keys/
$ cd laravel-json-exception-formatter
$ ./armadillo_standin.sh

$ vagrant up

$ ssh bslweb-laravel-json-exception-formatter-dev-node1
$ cd /app

$ composer install

$ logout

提交更改

使用 Git flow 工作流程来管理此包的开发。

离散更改应在 feature 分支中进行,这些分支是从 develop 创建并合并回的(在 develop 中可以直接进行小改动)。

准备发布一组功能/变更时,从develop分支创建一个release分支,根据需要更新文档,并与标记了master分支合并,使用语义版本(例如 v1.2.3)。

发布后,应将master分支与develop分支合并以重新启动流程。高影响力的bug可以在从master分支直接创建并合并到master分支(然后合并到develop分支)的hotfix分支中解决。

问题跟踪

与该包相关的所有问题、bug、改进、疑问、建议和其他任务都通过BAS Web & Applications Team Jira项目(BASWEB)进行管理。

清理

要移除开发VM

vagrant halt
vagrant destroy

然后可以安全地按正常方式删除laravel-json-exception-formatter目录。

许可证

版权所有 2014 NERC BAS。根据MIT许可证授权,详情请见LICENSE文件。