From 6e19517593c2a3129a23bbc5b408c06a21288e0d Mon Sep 17 00:00:00 2001 From: "J. King" Date: Mon, 25 Sep 2017 08:15:39 -0400 Subject: [PATCH] Return correct TTRSS error bodies even for HTTP-level errors TTRSS' error message for these cases (NOT_LOGGED_IN) is not especially helpful, but that's what it returns, so that's what we should return, albeit with correct HTTP status codes. --- lib/REST/TinyTinyRSS/API.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/REST/TinyTinyRSS/API.php b/lib/REST/TinyTinyRSS/API.php index a6cf3638..3e271867 100644 --- a/lib/REST/TinyTinyRSS/API.php +++ b/lib/REST/TinyTinyRSS/API.php @@ -14,6 +14,11 @@ use JKingWeb\Arsse\REST\Response; class API extends \JKingWeb\Arsse\REST\AbstractHandler { const LEVEL = 14; const VERSION = "17.4"; + const FATAL_ERR = [ + 'seq' => null, + 'status' => 1, + 'content' => ['error' => "NOT_LOGGED_IN"], + ]; const OVVERIDE = [ 'auth' => ["login"], ]; @@ -24,17 +29,17 @@ class API extends \JKingWeb\Arsse\REST\AbstractHandler { public function dispatch(\JKingWeb\Arsse\REST\Request $req): Response { if ($req->method != "POST") { // only POST requests are allowed - return new Response(405, "", "", ["Allow: POST"]); + return new Response(405, self::FATAL_ERR, "application/json", ["Allow: POST"]); } if ($req->body) { // only JSON entities are allowed if (!preg_match("<^application/json\b|^$>", $req->type)) { - return new Response(415, "", "", ['Accept: application/json']); + return new Response(415, self::FATAL_ERR, "application/json", ['Accept: application/json']); } $data = @json_decode($req->body, true); if (json_last_error() != \JSON_ERROR_NONE || !is_array($data)) { // non-JSON input indicates an error - return new Response(400); + return new Response(400, self::FATAL_ERR); } // layer input over defaults $data = array_merge([ @@ -76,7 +81,7 @@ class API extends \JKingWeb\Arsse\REST\AbstractHandler { } } else { // absence of a request body indicates an error - return new Response(400); + return new Response(400, self::FATAL_ERR); } }