I am working on a PHP library, which may be used in various environments by different PHP projects and I'm trying to be as minimalistic as possible.
In some circumstances I have to throw exceptions, for instance.
throw Exception('template has invalid tag');
The error like that is not very useful without the name of the tag:
throw Exception('template has invalid tag: '.$tag);
This would would be hard to localize and may result all sorts of injection problems.
QUESTION: What is the best way to pass extra variables with Exception in PHP?
(note: my library perform SQL Query building and I would prefer it to focus on the task, not solving problems with Exceptions)
The internationalization is not the responsibility of your library. Create one or more
Exceptionclasses for your project (throwing\Exceptionis not recommended as it is too generic) and let them store the parameters in properties.Let the error messages as they are now but also pass the values as arguments to the constructors of the new exceptions. Provide getters for them. The error messages are for the developers. The code that uses your library must catch the exceptions and display the error messages that are appropriate for them. They should not display the raw message you provide, anyway.
For example, you declare in your library:
The application that uses your library: