qubus / error
Package for Error handling.
Requires
- php: >=8.2
- qubus/log: ^3
Requires (Dev)
- phpunit/phpunit: ^9.0
- qubus/qubus-coding-standard: ^1.1
- squizlabs/php_codesniffer: ^3.5
This package is auto-updated.
Last update: 2024-12-07 01:44:59 UTC
README
Error handling with PSR-3 logging for catching exceptions as well as errors. There is also an error class to use as an alternative to throwing an exception.
Example
use Qubus\Error\Error; use function Qubus\Error\Helpers\is_error; function get_current_user(string|int $id = null) { if(null === $id) { return new Error('User id cannot be null.'); } } $user = get_current_user('56'); if(is_error($user)) { // log error message or send error flash message back to the user. }
Requirements
- PHP 8.2+
Installation
$ composer require qubus/error
Exceptions vs. Errors
There is a healthy discussion among developers whether to use exceptions or error return codes. There are pros and cons to both sides. No matter what side you fall on, the articles below are a great read with a dose of great examples as to the proper use of exceptions.
- Exception Handling Considered Harmful
- Why is exception handling bad?
- "The rule of thumb is that exceptions should flag exceptional conditions, and that you should not use them for control of program flow."
- Why I Hate Exceptions
- Structured Exception Handling Considered Harmful
- Exceptions
- Why You Should Avoid Using Exceptions as the Control Flow in Java
- Exceptional Exceptions, Demystified - Part 1
- Exceptional Exceptions, Demystified - Part 2
- Exceptional Exceptions Demystified - Part 3
Author Statement
I am of the mind that if a function is to return something, then it should return, not throw. If an exception is exceptional, it should be caught and logged. I know that I don't do this perfectly, but it is my goal to go through all of my code and make sure that exceptions are exceptional and not unexceptional.