How do we deal with custom error handling in real world?

83 Views Asked by At

I'll start with a minimal example. Suppose we have the following dependency in our project:

<?php
namespace VeryUsefulFilesystemLibrary;

function read(string $filepath): string|false
{
    return file_get_contents($filepath);
}

And our project looks like:

<?php
require 'vendor/autoload.php';

$filedata = VeryUsefulFilesystemLibrary\read('/path/to/file');

// do something depending on $filedata (signature: string|false)

At some point (perhaps early in development), we decided to use a custom error handler like filp/whoops:

<?php
require 'vendor/autoload.php';

(new Whoops\Run)
    ->pushHandler(new Whoops\Handler\PlainTextHandler)
    ->register();

$filedata = VeryUsefulFilesystemLibrary\read('/path/to/file');

// how about Whoops\Exception\ErrorException now? :)

Now all potential warnings that were previously logged but processed correctly, now killing the app. I could use try/catch, but, firstly, e.g. fopen() with try/catch looks extremely weird, and secondly, I have to think more. Much more. And… I can't even find the words, how… wrong is this?

Personally, I would prefer that absolutely any problem in PHP is an exception. Exactly how Whoops does to him. But I don't understand how to work in this way with a language that behaves differently by default, and therefore doesn't have the necessary documentation (the same applies to dependencies).

Am I missing something?

0

There are 0 best solutions below