Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Wrong file/line in exceptions created by "factory" methods #4110

Closed
WinterSilence opened this issue Jan 16, 2021 · 0 comments · Fixed by #4118
Closed

Bug: Wrong file/line in exceptions created by "factory" methods #4110

WinterSilence opened this issue Jan 16, 2021 · 0 comments · Fixed by #4118
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@WinterSilence
Copy link
Contributor

WinterSilence commented Jan 16, 2021

Direction

Wrong $file, $line properties in exceptions created by "factory" methods.

Describe the bug

$file/$line where the exception was created.

For example, class CodeIgniter\Exceptions\FrameworkException

// file "index.php", line 10:
$e =  FrameworkException::forEnabledZlibOutputCompression();
var_dump($e->getFile(), $e->getLine()); // file '.../FrameworkException.php', line 26

because exception created in

return new static(lang('Core.enabledZlibOutputCompression'));

// file "index.php", line 10:
$e =  new FrameworkException(lang('Core.enabledZlibOutputCompression'));
var_dump($e->getFile(), $e->getLine()); // file 'index.php', line 10

CodeIgniter 4 version
4.04

Fixes

  1. Convert factory methods to Exception classes:
abstract class FrameworkException extends RuntimeException implements ExceptionInterface
{
}

class EnabledZlibOutputCompressionException extends FrameworkException
{
    public function __construct(string $message = 'Core.enabledZlibOutputCompression', int $code = 0, Throwable $previous = null)
    {
          parent::__construct(lang($message), $code, $previous);
    }
}
  1. Set $file/$line by debug_backtrace(), for example:
public static function forEnabledZlibOutputCompression()
{
	 $e = new static(lang('Core.enabledZlibOutputCompression'));
         ['file' => $e->file, 'line' => $e->line] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0];
        return $e;
}
@WinterSilence WinterSilence added the bug Verified issues on the current code behavior or pull requests that will fix them label Jan 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant