holicz/simple-exception

简单的基异常类

v2.0.1 2021-03-31 19:56 UTC

This package is auto-updated.

Last update: 2024-09-29 04:49:03 UTC


README

提供公共和私有上下文的简单基异常类,灵感来源于 https://github.com/EasyCorp/EasyAdminBundle

安装和使用

使用 composer 安装

composer require holicz/simple-exception

您的异常类

<?php

namespace App\Exception;

use holicz\SimpleException\BaseException;
use holicz\SimpleException\ExceptionContext;

class CouldNotRemoveArticleException extends BaseException
{
    public function __construct(int $id)
    {
        $exceptionContext = new ExceptionContext(
            'There was an error during article removal. Please try again later.',
            sprintf('Could not delete article with id %d', $id),
            500 // HTTP status code
        );
    
        parent::__construct($exceptionContext);
    }
}

您的代码

try {
    ...
} catch (MyException $e) {
    // Available methods
    $e->getPublicMessage(); // Show to user
    $e->getDebugMessage(); // Log
    $e->getStatusCode();
}