0

Ok, i'm very confused. I'm using the Slim framework and I have following Code:

1 $app->post("/user/login", function (Request $request, Response $response) {
2    $resp = null;
3    try {
4       if (!array_key_exists("password", $request->getParams()) || !array_key_exists("username", $request->getParams())) {
5            throw new LiberInvalidRequestException("Not all required parameters were passed.");
6        }
7        $user = new User($this->db, [
8            'username' => $request->getParam('username'),
9            'password' => $request->getParam('password')
10        ]);
11        try {
12            $resp['logincode'] = $user->login();
13            $resp['success'] = true;
14        } catch (LiberAuthenticationException $exception) {
15            $resp['success'] = false;
16            $resp['errormessage'] = $exception->getMessage();
17        }
18    } catch (LiberInvalidRequestException $exception) {
19        $resp['success'] = false;
20        $resp['errormessage'] = $exception->getMessage();
21    } finally {
22        return $response->withJson($resp);
23    }
24 });

When I call this route with the right params i get a null response. In my opinion this shouldn't be possible because in every possible way something is assigned to $resp. When i go through this function with the debugger I can go until line 12 then the debugger jumps to the finally block (line 22).

How is this even possible? Shouldn't the execution continue or go to the catch block?

Aaronmacaron
  • 309
  • 3
  • 10
  • It's possible if some exception other than `LiberAuthenticationException` or `LiberInvalidRequestException` is thrown… namespace issues perchance? – deceze Apr 14 '17 at 20:41
  • @deceze I don't think there is another exception thrown. I discovered that the Point when the execution jumps to the finally block is a include statement inside the composer autoloader. Include statements don't throw Exceptions IIRC. – Aaronmacaron Apr 14 '17 at 20:54
  • A `try` inside a `try` would only add to the confusion. Please see [this](http://stackoverflow.com/q/8439581/2298301) post on how multiple `catch` blocks could be written for a single `try` - if the need be. – Dhruv Saxena Apr 14 '17 at 20:58
  • @DhruvSaxena The question you posted the link for is about how to write multiple catches in one block. I have multiple try blocks that do different things. Also i don't think that this has very much to do with my question. – Aaronmacaron Apr 14 '17 at 21:14

1 Answers1

2

Ok I found the solution to the problem. As @deceze said there was a Exception thrown that the catch block didn't catch. Since i use PHP7.1 include statements can throw Exceptions. That's the reason why a Exception was thrown in the first place.

Aaronmacaron
  • 309
  • 3
  • 10