[Corona] Catch exceptions in the FrontController

Review Request #554 — Created May 7, 2018 and submitted

pprkut
Lunr
pprkut:exceptions
576
df96749...
lunr

Catch exceptions in a RequestResultHandler class, which can be used by either a FrontController or a Controller class.
The RequestResultHandler sets the proper response codes, which should remove boiler plate code from Controllers.

Unit tests

  • 0
  • 0
  • 13
  • 3
  • 16
Description From Last Updated
pprkut
smillernl
  1. 
      
  2. I'd imagine we'd want to know the exception class here. Maybe make it like$e::class . ': ' . $e->getMessage();

    1. I'm fine seeing exception names in log messages, but don't want to see them in the JSON output of webservices, so not here at least.

  3. 
      
tardypad
  1. 
      
  2. to support application specific error, we may create a specific constructor here which would allow an application_error_code parameter

    1. Yes, indeed. We don't even need a special constructor, we just jave to not use a special constructor for the child classes.
      We do need a special getter method here though.

  3. src/Lunr/Corona/FrontController.php (Diff revision 1)
     
     

    I didn't understand the review comment about the need to make this a Trait

    1st: just to confirm the motivation behind not making that exception handling and result setting directly in the FrontController class
    Is it to be flexible and leave the choice of the result handling to the "project" using it?

    2nd: I am not a big fan of using traits.
    They caused too many implementations and testing issues in the past.
    I don't see much of their benefits over using a normal RequestResultHandler class there

    1. 1st

      No, it's to allow request handling also outside of the FrontController. Basically, everywhere where you mock request values you want to catch exceptions and set response values.

      2nd

      Me neither, but in this case is seemed like the better option. It's not really meant to be injectable or overrideable. If you need additional handling of something you can still do that
      in the controller. We could probably get the same behavior by making the class final. That would leave the need for a locator recipe, but that wouldn't be too bad. I'll think that over.

  4. src/Lunr/Corona/FrontController.php (Diff revision 1)
     
     
     
     
     

    This looks weird now: a public function just calling an private function, which is itself only meant for this one.
    We may actually now better move that whole function into the Handler itself, no?

    Not sure why you don't pass the request params as a parameter directly for example too.

    1. dispatch() is really specific to the FrontController. You wouldn't call that in a controller. handle_request() encapsulates the common parts.
      I'm fine with passing the params as arguments here though.

  5. 
      
pprkut
pprkut
tardypad
  1. 
      
  2. $code should be renamed to $app_code
    as in the HTTPException class
    Otherwise this is quite confusing

    Same in other exception sub classes

  3. src/Lunr/Corona/Exceptions/HttpException.php (Diff revision 3)
     
     
     
     
     
     
     

    This is not matching the code above in the set_result function where no application code is expected to be represented by NULL

    1. Every HttpException will return an application error code != 0, and any other Exception will not pass a value at all to set_result() (hence the NULL default)

      Essentially, the application error code is an integer for exceptions and a nullable integer in set_result(). Not seeing the problem.

    2. With the current code, if I throw a BadRequestException without any app code, it will lead to an error_info being set in the result to 400
      while we don't want to have this value set

    3. It's the current behavior as well, it just happens elsewhere (in the JSONView/MsgpackView). I just figured it's easier/better to handle here than there. But if you think there's a case for keeping the application code NULL I'm listening :)

  4. src/Lunr/Corona/FrontController.php (Diff revision 3)
     
     

    maybe a better specific naming would be better here
    request_result_handler

    1. I picked $handler since anything else seemed awefully repetitive. $this->request_result_handler->handle_request().... meh.

      IMHO this is a non-issue until there's more than one possible option.

    2. Whatever indeed
      I'd say the problem is mainly the naming of the handle_request function itself
      but I couldn't come up with something better than vague names such as "run" or "process" (which is what the function is also doing)

    3. Not opposed to using process() if you think that's better

  5. not sure I understand why you put it as "final".
    What problems can happen if it would be extended?

    1. It's mostly an indicator that the class shouldn't need extending. It's really generic, and if you need different behavior it's likely you need completely different behavior, in which case you'd replace it entirely anyway. Anyway, I dropped the final classifier.

  6. 
      
pprkut
smillernl
  1. Ship It!
  2. 
      
pprkut
Review request changed

Status: Closed (submitted)

Change Summary:

Merged into master

Loading...