Try/Catch

PHOTO EMBED

Wed Jul 07 2021 19:35:03 GMT+0000 (Coordinated Universal Time)

Saved by @slendabilt #php

public function readAction($request, $response, $userId, GetTicketsService $getTicketsService)
    {
        try {
            $role = $this->container->has('jwt') ?  $this->container->get('jwt')->role : 'admin';
            $status = $request->getParam('status') ? $request->getParam('status') : null;
            $serviceRequest = new GetTicketsRequest([
                'userId' => $userId,
                'role' => $role,
                'status' => $status,
            ]);
            /*
             * Tickets are already an array
             */
            $tickets = $getTicketsService->execute($serviceRequest);
            return $response->withJson($tickets);
        } catch (\Exception $e) {
            $code = (int) $e->getCode() > 200 && (int) $e->getCode() <= 500 ? (int) $e->getCode() : 500;
            return $response->withStatus($code)->withJson(ErrorResponse::createFromException($e));
        }
    }
content_copyCOPY

The code goes in the try section. The catch handles a situation where the code breaks