2

I have added a new configurable variable to config/app.php that can be set in the .env file but this one must be in the form of a valid regex pattern (it is used by a middleware layer in my app).

It is possible that a future developer might not realise it has to be a proper regex pattern and just supplies a string which will cause them all sorts of headaches (this "future developer" may actually be me in 2 years time). In which case I would like to throw a nice error message explaining that they need to fix this variable. I presume the middleware that uses it is the best place to do the validation but my question is how should I throw the error?

  • An exception? If so, which one's appropriate?
  • Set a 4xx status code in the header, dump a plain text message and exit? (afterall this variable is only going to be changing at the time of installation so interrupting service won't matter)
  • Just let the default PHP error happen when the invalid pattern is fed into a preg_* function
  • Some other built-in Laravel 5 feature that I don't know about?

I'd love to know people's opinions.

Martin Joiner
  • 3,529
  • 2
  • 23
  • 49
  • How do you plan on determining if the string provided is not a regex? – Alex Howansky May 04 '17 at 14:38
  • @AlexHowansky By using the accepted answer on this question http://stackoverflow.com/questions/8825025/test-if-a-regular-expression-is-a-valid-one-in-php – Martin Joiner May 04 '17 at 14:40
  • Would it make sense to log the issue and then change the regex to a match all like `/./` when this occurs? Or do you want the app to actually fully halt? – Alex Howansky May 04 '17 at 14:50
  • Yeah I think I'd want the app to fully halt, the developer needs to know they've tripped up so as many alarm bells as possible should ring. – Martin Joiner May 04 '17 at 14:56
  • In that case, I'd just throw an exception and let the app act as normal when an uncaught exception is encountered. (Which would likely be something like, log the issue and display a nice error.) – Alex Howansky May 04 '17 at 14:57
  • Cool, thanks for helping me out. – Martin Joiner May 04 '17 at 15:02

0 Answers0