Skip to content

Commit c8b6205

Browse files
authored
feat(api): extend structured error handling to all error responses (#20)
2 parents bbef8df + 060105d commit c8b6205

File tree

4 files changed

+7
-7
lines changed

4 files changed

+7
-7
lines changed

src/jsons/search.nim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ proc createJsonApiSearchRouter*(cfg: Config) =
1414
get "/api/search?":
1515
let q = @"q"
1616
if q.len > 500:
17-
respJsonError "Search input too long."
17+
respJsonError("Search input too long.", "invalid_input", Http400)
1818

1919
let
2020
prefs = cookiePrefs()
@@ -23,7 +23,7 @@ proc createJsonApiSearchRouter*(cfg: Config) =
2323
case query.kind
2424
of users:
2525
if "," in q:
26-
respJsonError "Invalid search input"
26+
respJsonError("Invalid search input", "invalid_input", Http400)
2727
var users: Result[User]
2828
try:
2929
users = await getGraphUserSearch(query, getCursor())
@@ -32,10 +32,10 @@ proc createJsonApiSearchRouter*(cfg: Config) =
3232
respJsonSuccess formatUsersAsJson(users)
3333
of tweets:
3434
let timeline = await getGraphTweetSearch(query, getCursor())
35-
if timeline.content.len == 0: respJsonError "No results found"
35+
if timeline.content.len == 0: respJsonError("No results found", "no_results", Http200)
3636
respJsonSuccess formatTimelineAsJson(timeline)
3737
else:
38-
respJsonError "Invalid search"
38+
respJsonError("Invalid search", "invalid_input", Http400)
3939

4040
get "/api/hashtag/@hash":
4141
redirect("/search?q=" & encodeUrl("#" & @"hash"))

src/jsons/status.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ proc createJsonApiStatusRouter*(cfg: Config) =
5252
let id = @"id"
5353

5454
if id.len > 19 or id.any(c => not c.isDigit):
55-
respJsonError "Invalid tweet ID"
55+
respJsonError("Invalid tweet ID", "invalid_input", Http400)
5656

5757
let conv = await getTweet(id, getCursor())
5858

src/jsons/timeline.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ proc createJsonApiTimelineRouter*(cfg: Config) =
155155

156156
if query.fromUser.len != 1:
157157
var timeline = await getGraphTweetSearch(query, after)
158-
if timeline.content.len == 0: respJsonError "No results found"
158+
if timeline.content.len == 0: respJsonError("No results found", "no_results", Http200)
159159
timeline.beginning = true
160160
respJsonSuccess formatTimelineAsJson(timeline)
161161
else:

src/routes/router_utils.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# SPDX-License-Identifier: AGPL-3.0-only
22
import strutils, sequtils, uri, tables, json
3-
from jester import Request, cookies
3+
from jester import Request, cookies, HttpCode, Http200
44

55
import ../views/general
66
import ".."/[utils, prefs, types]

0 commit comments

Comments
 (0)