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

All exceptions should inherit from a base PHPEncoder\Exception #9

Open
jmwebservices opened this issue Oct 8, 2018 · 2 comments
Open

Comments

@jmwebservices
Copy link

jmwebservices commented Oct 8, 2018

Thanks for this project. I have been using it for several months to prettify a configuration array that is modified by a UI and written to a file.

It would be nice if all exceptions thrown by PHPEncoder would inherit from a base PHPEncoder\Exception class. Currently, InvalidOptionException is the only PHPEncoder specific exception while InvalidArgumentException and RuntimeException are thrown directly. Implementing this recommendation will make it possible to distinguish between PHPEncoder exceptions and other exceptions thrown by an app using PHPEncoder (see example below).

public function write( $content ) : bool
{

	try
	{

		if( !$this->validate( $content ) )
			throw new APP_Exception( 'SOME USEFUL MESSAGE.' );

		$put = ( new \Riimu\Kit\PHPEncoder\PHPEncoder )->encode( $content );

		if( file_put_contents( $this->path(), $put ) === false )
			throw new APP_Exception( 'SOME USEFUL MESSAGE.' );

	}
	catch( APP_Exception $e )
	{
		// do something with APP thrown exception
	}
	catch( \Riimu\Kit\PHPEncoder\Exception $e )
	{
		// do something with PHPEncoder thrown exception
	}

	return true;
}
@Riimu
Copy link
Owner

Riimu commented Oct 10, 2018

Not an unreasonable request. I always struggle with how to properly organize and categorize exceptions in libraries. Now that I look closely into this project, it seems I'm already a bit inconsistent with the existing exceptions.

I'll keep this in mind for future improvements, but I won't promise an immediate fix as proper organization requires a bit of a BC break.

@jmwebservices
Copy link
Author

jmwebservices commented Oct 10, 2018

@Riimu Thanks for responding! It's very discouraging when one submits issues or PRs to a repo and the owner ignores it completely!

As for implementing this in a BC way, you could do something like the following:

namespace Riimu\Kit\PHPEncoder;

interface Exception{}

class InvalidOptionException extends \InvalidArgumentException implements Exception{}
class RuntimeException extends \RuntimeException implements Exception{}
class InvalidArgumentException extends \InvalidArgumentException implements Exception{}

This configuration will allow us to catch \RuntimeException and \InvalidArgumentException exceptions (for BC purposes) and also \Riimu\Kit\PHPEncoder\Exception exceptions.

I am pretty sure empty interfaces are frowned upon, but it looks like a good fit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants