ptdot/errormessage

Laravel 简单错误信息显示从异常

1.0.1 2019-10-01 05:07 UTC

This package is auto-updated.

Last update: 2024-09-29 05:28:24 UTC


README

有时在开发模式下,你可能想显示异常信息以了解你的Web应用程序发生了什么。但在生产环境中,你可能想显示通用信息而不是直接显示异常信息。Laravel错误信息显示器是一个基于APP_DEBUG条件的显示异常信息或跟踪字符串的包。

要求

  • php 7.0+
  • Laravel 5.5+

安装

通过composer安装包。

composer require ptdot/errormessage

接下来,如果你使用的是Laravel 5.5以下版本,请将服务提供者和外观包含在你的config/app.php文件中。

'providers' => [
    Ptdot\ErrorMessage\ErrorMessageServiceProvider::class,
],

'aliases' => [
    'ErrorMessage' => Ptdot\ErrorMessage\ErrorMessage::class,
]

自Laravel 5.5+以来使用包发现,因此无需手动在app.php中插入服务提供者和外观。

配置

使用命令发布配置

php artisan vendor:publish

config/errormessage.php中设置你的默认错误信息。

基本用法

你可以使用这个包通过你的REST API或闪存消息来处理异常显示。

<?php

// default usage using facade
try {
    throw new \Exception('This is exception message');
} catch (\Exception $exception) {
    return response()->json([
        'errors' => ErrorMessage::displayExceptionMessage($exception)
    ], 500);
}

如果你想要调试并且需要显示traceAsString选项,你可以调用traceAsString()方法

// you may need traceAsString option enabled using this way:
return response()->json([
        'errors' => ErrorMessage::traceAsString()->displayExceptionMessage($exception)
    ], 500);

想要覆盖默认消息?不用担心,这个包能够做到这一点。

// Need to override exception message? Don't worry
return response()->json([
        'errors' => ErrorMessage::displayExceptionMessage($exception, 'exception message will be overrided with this')
    ], 500);

贡献

如果你想要帮助这个包变得更好、更有用,请随时报告问题或合并请求。