From 3c689c09ed09d16f2be1b4ce7f370f24e35b2193 Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Fri, 23 Sep 2016 16:55:02 +0100 Subject: [PATCH] Much better json response code, add an extra try/catch --- app/Http/Controllers/MicropubController.php | 61 ++++++++------------- 1 file changed, 23 insertions(+), 38 deletions(-) diff --git a/app/Http/Controllers/MicropubController.php b/app/Http/Controllers/MicropubController.php index c1b6dc64..61e82f49 100644 --- a/app/Http/Controllers/MicropubController.php +++ b/app/Http/Controllers/MicropubController.php @@ -57,17 +57,15 @@ class MicropubController extends Controller if (array_search('post', $scopes) !== false) { $clientId = $tokenData->getClaim('client_id'); if (($request->input('h') == 'entry') || ($request->input('type')[0] == 'h-entry')) { - $note = $this->noteService->createNote($request, $clientId); - $content = <<longurl" -} -EOD; - - return (new Response($content, 201)) - ->header('Location', $note->longurl) - ->header('Content-Type', 'application/json'); + try { + $note = $this->noteService->createNote($request, $clientId); + } catch (Exception $exception) { + return response()->json(['error' => true], 400); + } + return response()->json([ + 'response' => 'created', + 'location' => $note->longurl + ], 201)->header('Location', $note->longurl); } if ($request->input('h') == 'card' || $request->input('type')[0] == 'h-card') { try { @@ -75,40 +73,27 @@ EOD; } catch (Exception $exception) { return response()->json(['error' => true], 400); } - $content = <<longurl" -} -EOD; - return (new Response($content, 201)) - ->header('Location', $place->longurl) - ->header('Content-Type', 'application/json'); + return response()->json([ + 'response' => 'created', + 'location' => $place->longurl + ], 201)->header('Location', $place->longurl); } } } - $content = <<<'EOD' -{ - "response": "error", - "error": "invalid_token", - "error_description": "The token provided is not valid or does not have the necessary scope", -} -EOD; - return (new Response($content, 400)) - ->header('Content-Type', 'application/json'); + return response()->json([ + 'response' => 'error', + 'error' => 'invalid_token', + 'error_description' => 'The token provided is not valid or does not have the necessary scope' + ], 400); } - $content = <<<'EOD' -{ - "response": "error", - "error": "no_token", - "error_description": "No OAuth token sent with request" -} -EOD; - return (new Response($content, 400)) - ->header('Content-Type', 'application/json'); + return response()->json([ + 'response' => 'error', + 'error' => 'no_token', + 'error_description' => 'No OAuth token sent with request' + ], 400); } /**