laravel-api-server/error-handler 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

laravel-api-server/error-handler

Composer 安装命令:

composer require laravel-api-server/error-handler

包简介

This package provides advanced errorhandling for Laravel.

README 文档

README

This package provides advanced errorhandling for Laravel.

Using this package

Register service provider in config/app.php by adding the following line to the providers-array:

        ApiServer\ErrorHandler\Providers\ModuleServiceProvider::class,

Adjust your render()-method in àpp/Exceptions/Handler.php to become:

public function render($request, Exception $exception)
{
    $errorHandler = \App::make(ErrorHandler::class);
    return $errorHandler->handle($exception, $request);
}

Next create a directory structure that looks like this:

app/Exceptions/Handler/<ResponseType>/
app/Exceptions/Handler/<WebResponseInExample>/
app/Exceptions/Handler/<JsonApiResponseInExample>/

Inside these directories you create your exception handlers:

<?php

namespace Netmon\Devices\Exceptions\Handler\WebResponse;

use Exception;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use ApiServer\ErrorHandler\Contracts\ExceptionHandlerInterface;
use Netmon\Devices\Exceptions\Exceptions\ErrorException;

class ErrorExceptionHandler implements ExceptionHandlerInterface
{
    public function manages(Exception $e, Request $request): bool {
        return ($e instanceof ErrorException);
    }

    public function handle(Exception $e, Request $request): Response {
        $status = 200;
        $message = "This is an error example"

        return response($message, $status);
    }
}

You can register the error handler by creating a service provider with a register()-method:

public function register()
{
    // get ErrorHandler singleton
    $errorHandler = \App::make(ErrorHandler::class);

    // register custom error handlers
    $errorHandler->registerHandler(new ErrorExceptionHandler);
}

If you want to make your Handler apply to just some special named routes only, you can pass the matching routes like this during registration:

    $errorHandler->registerHandler(new ErrorExceptionHandler, ['api.', 'web.']);

If you want to make shure that you handler gets checked after all other handlers have been checked you can append the handler by adding true as third parameter (designed for registering fallback handlers in a modularized environment):

    $errorHandler->registerHandler(new ErrorExceptionHandler, [], true);

Contributing

Submitting patches

Patches can be submitted using the Merge-Request link of our gitlab.

Mailinglist

https://lists.ffnw.de/mailman/listinfo/netmon-dev

Credits

Ideas where thankfully token from the tobscure/json-api library.

License

See License

统计信息

  • 总下载量: 1.28k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-06-08

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固