Return json for all the requests the newnote.js makes, this allows for better error handling on the frontend

This commit is contained in:
Jonny Barnes 2016-09-23 12:26:43 +01:00
parent 5d410352db
commit 4133efb8a8

View file

@ -204,34 +204,37 @@ class MicropubClientController extends Controller
*/ */
public function postNewPlace(Request $request) public function postNewPlace(Request $request)
{ {
if ($request->session()->has('token') === false) {
return response()->json([
'error' => true,
'error_description' => 'No known token',
], 400);
}
$domain = $request->session()->get('me'); $domain = $request->session()->get('me');
$token = $request->session()->get('token'); $token = $request->session()->get('token');
$micropubEndpoint = $this->indieAuthService->discoverMicropubEndpoint($domain, $this->indieClient); $micropubEndpoint = $this->indieAuthService->discoverMicropubEndpoint($domain, $this->indieClient);
if (! $micropubEndpoint) { if (! $micropubEndpoint) {
return (new Response(json_encode([ return response()->json([
'error' => true, 'error' => true,
'message' => 'Could not determine the micropub endpoint.', 'error_description' => 'Could not determine the micropub endpoint.',
]), 400)) ], 400);
->header('Content-Type', 'application/json');
} }
$place = $this->postPlaceRequest($request, $micropubEndpoint, $token); $place = $this->postPlaceRequest($request, $micropubEndpoint, $token);
if ($place === false) { if ($place === false) {
return (new Response(json_encode([ return response()->json([
'error' => true, 'error' => true,
'message' => 'Unable to create the new place', 'error_description' => 'Unable to create the new place',
]), 400)) ], 400);
->header('Content-Type', 'application/json');
} }
return (new Response(json_encode([ return response()->json([
'url' => $place, 'url' => $place,
'name' => $request->input('place-name'), 'name' => $request->input('place-name'),
'latitude' => $request->input('place-latitude'), 'latitude' => $request->input('place-latitude'),
'longitude' => $request->input('place-longitude'), 'longitude' => $request->input('place-longitude'),
]), 200)) ]);
->header('Content-Type', 'application/json');
} }
/** /**
@ -285,12 +288,22 @@ class MicropubClientController extends Controller
$latitude, $latitude,
$longitude $longitude
) { ) {
if ($request->session()->has('token') === false) {
return response()->json([
'error' => true,
'error_description' => 'No known token',
], 400);
}
$domain = $request->session()->get('me'); $domain = $request->session()->get('me');
$token = $request->session()->get('token'); $token = $request->session()->get('token');
$micropubEndpoint = $this->indieAuthService->discoverMicropubEndpoint($domain, $this->indieClient); $micropubEndpoint = $this->indieAuthService->discoverMicropubEndpoint($domain, $this->indieClient);
if (! $micropubEndpoint) { if (! $micropubEndpoint) {
return; return response()->json([
'error' => true,
'error_description' => 'No known endpoint',
], 400);
} }
try { try {
@ -299,7 +312,10 @@ class MicropubClientController extends Controller
'query' => ['q' => 'geo:' . $latitude . ',' . $longitude], 'query' => ['q' => 'geo:' . $latitude . ',' . $longitude],
]); ]);
} catch (\GuzzleHttp\Exception\BadResponseException $e) { } catch (\GuzzleHttp\Exception\BadResponseException $e) {
return; return response()->json([
'error' => true,
'error_description' => 'The endpoint returned a non-good response',
], 400);
} }
return (new Response($response->getBody(), 200)) return (new Response($response->getBody(), 200))