承接 beter/exception-with-context 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

beter/exception-with-context

最新稳定版本:1.0.1

Composer 安装命令:

composer require beter/exception-with-context

包简介

Interface, Trait and Class of the exception with a context

README 文档

README

Requirements

PHP >= 7.4.0

Installation

The preferred way to install this extension is through composer.

Either run

composer require beter/exception-with-context 

or add

"beter/exception-with-context": "~1.0.0" // put the actual version 

to the require section of your composer.json.

Usage

Usage of the ExceptionWithContext:

use Beter\ExceptionWithContext\ExceptionWithContext; $exceptionCode = 0; $previousException = null; $context = [ 'userIp' => '1.2.3.4', 'userId' => 1, 'request' => [ 'headers' => [ /* put headers data, for example */ ] ], ]; $e = new ExceptionWithContext('Action is not allowed for the user', $exceptionCode, $previousException, $context); // or you may not to pass the context $e = new ExceptionWithContext('Action is not allowed for the user', $exceptionCode, $previousException); // or even skip $exceptionCode and $previousException $e = new ExceptionWithContext('Action is not allowed for the user'); // or set context via setContext method call $e = new ExceptionWithContext('Action is not allowed for the user'); $e->setContext($context); // and get context back var_dump($e->getContext());

You may create a chain of exceptions too:

use Beter\ExceptionWithContext\ExceptionWithContext; try { do_smth(); } catch (\Throwable $catched) { $e = new ExceptionWithContext('Something went wrong', 0, $catched, ['key' => 'value']); }

You may redefine your base exceptions and add context trait to them. They will behave the same way.

use Beter\ExceptionWithContext\ExceptionWithContextInterface; use Beter\ExceptionWithContext\ExceptionWithContextTrait; class CustomException extends \Exception implements ExceptionWithContextInterface { use ExceptionWithContextTrait; public function __construct($message = "", $code = 0, Throwable $previous = null, array $context = []) { $this->context = $context; parent::__construct($message, $code, $previous); } } $e = new CustomException('Message text', 0, null, ['key' => 'value']); $e->setContext(['newkey' => 'newvalue']); var_dump($e->getContext());

Also, you may implement your own custom exceptions, you even don't need to use traits. Just implement Beter\Exception\ExceptionWithContextInterface.

Ideas for usage

  • add context to log messages;
  • protect from flooding exception message with placeholded values right into the message;
  • pass more data to sentry/logentries/datadog/newrelic and so on.

You need to implement that support by yourself. There are only ideas.

Development and testing

Follow the development and testing doc.

Related projects

These projects use beter/exception-with-context:

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固