From a5f75dab36145cb6564f6b1cd36c328c1d6fdd25 Mon Sep 17 00:00:00 2001 From: whatdoineed2do Date: Mon, 25 Oct 2021 16:36:51 +0100 Subject: [PATCH] [jsonapi] /api/library/*/{id} returns 404 (instead of 500) on not finding item (#1326) Co-authored-by: whatdoineed2do/Ray --- src/httpd_jsonapi.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/httpd_jsonapi.c b/src/httpd_jsonapi.c index 190d28a8..ea00e4f3 100644 --- a/src/httpd_jsonapi.c +++ b/src/httpd_jsonapi.c @@ -491,13 +491,14 @@ fetch_artists(struct query_params *query_params, json_object *items, int *total) } static json_object * -fetch_artist(const char *artist_id) +fetch_artist(bool *notfound, const char *artist_id) { struct query_params query_params; json_object *artist; struct db_group_info dbgri; int ret = 0; + *notfound = true; memset(&query_params, 0, sizeof(struct query_params)); artist = NULL; @@ -512,6 +513,7 @@ fetch_artist(const char *artist_id) if ((ret = db_query_fetch_group(&query_params, &dbgri)) == 0) { artist = artist_to_json(&dbgri); + notfound = false; } error: @@ -558,13 +560,15 @@ fetch_albums(struct query_params *query_params, json_object *items, int *total) } static json_object * -fetch_album(const char *album_id) +fetch_album(bool *notfound, const char *album_id) { struct query_params query_params; json_object *album; struct db_group_info dbgri; int ret = 0; + *notfound = true; + memset(&query_params, 0, sizeof(struct query_params)); album = NULL; @@ -579,6 +583,7 @@ fetch_album(const char *album_id) if ((ret = db_query_fetch_group(&query_params, &dbgri)) == 0) { album = album_to_json(&dbgri); + *notfound = false; } error: @@ -621,13 +626,15 @@ fetch_playlists(struct query_params *query_params, json_object *items, int *tota } static json_object * -fetch_playlist(uint32_t playlist_id) +fetch_playlist(bool *notfound, uint32_t playlist_id) { struct query_params query_params; json_object *playlist; struct db_playlist_info dbpli; int ret = 0; + *notfound = true; + memset(&query_params, 0, sizeof(struct query_params)); playlist = NULL; @@ -642,6 +649,7 @@ fetch_playlist(uint32_t playlist_id) if (((ret = db_query_fetch_pl(&query_params, &dbpli)) == 0) && (dbpli.id)) { playlist = playlist_to_json(&dbpli); + *notfound = false; } error: @@ -3003,13 +3011,14 @@ jsonapi_reply_library_artist(struct httpd_request *hreq) const char *artist_id; json_object *reply; int ret = 0; + bool notfound = false; if (!is_modified(hreq->req, DB_ADMIN_DB_UPDATE)) return HTTP_NOTMODIFIED; artist_id = hreq->uri_parsed->path_parts[3]; - reply = fetch_artist(artist_id); + reply = fetch_artist(¬found, artist_id); if (!reply) { ret = -1; @@ -3024,7 +3033,7 @@ jsonapi_reply_library_artist(struct httpd_request *hreq) jparse_free(reply); if (ret < 0) - return HTTP_INTERNAL; + return notfound ? HTTP_NOTFOUND : HTTP_INTERNAL; return HTTP_OK; } @@ -3151,13 +3160,14 @@ jsonapi_reply_library_album(struct httpd_request *hreq) const char *album_id; json_object *reply; int ret = 0; + bool notfound = false; if (!is_modified(hreq->req, DB_ADMIN_DB_UPDATE)) return HTTP_NOTMODIFIED; album_id = hreq->uri_parsed->path_parts[3]; - reply = fetch_album(album_id); + reply = fetch_album(¬found, album_id); if (!reply) { ret = -1; @@ -3172,7 +3182,7 @@ jsonapi_reply_library_album(struct httpd_request *hreq) jparse_free(reply); if (ret < 0) - return HTTP_INTERNAL; + return notfound ? HTTP_NOTFOUND : HTTP_INTERNAL; return HTTP_OK; } @@ -3269,6 +3279,7 @@ jsonapi_reply_library_tracks_get_byid(struct httpd_request *hreq) struct db_media_file_info dbmfi; json_object *reply = NULL; int ret = 0; + bool notfound = false; if (!is_modified(hreq->req, DB_ADMIN_DB_MODIFIED)) return HTTP_NOTMODIFIED; @@ -3292,6 +3303,7 @@ jsonapi_reply_library_tracks_get_byid(struct httpd_request *hreq) { DPRINTF(E_LOG, L_WEB, "Track with id '%s' not found.\n", track_id); ret = -1; + notfound = true; goto error; } @@ -3307,7 +3319,7 @@ jsonapi_reply_library_tracks_get_byid(struct httpd_request *hreq) jparse_free(reply); if (ret < 0) - return HTTP_INTERNAL; + return notfound ? HTTP_NOTFOUND : HTTP_INTERNAL; return HTTP_OK; } @@ -3577,6 +3589,7 @@ jsonapi_reply_library_playlist_get(struct httpd_request *hreq) uint32_t playlist_id; json_object *reply = NULL; int ret = 0; + bool notfound = false; if (!is_modified(hreq->req, DB_ADMIN_DB_UPDATE)) return HTTP_NOTMODIFIED; @@ -3599,7 +3612,7 @@ jsonapi_reply_library_playlist_get(struct httpd_request *hreq) } else { - reply = fetch_playlist(playlist_id); + reply = fetch_playlist(¬found, playlist_id); } if (!reply) @@ -3616,7 +3629,7 @@ jsonapi_reply_library_playlist_get(struct httpd_request *hreq) jparse_free(reply); if (ret < 0) - return HTTP_INTERNAL; + return notfound ? HTTP_NOTFOUND : HTTP_INTERNAL; return HTTP_OK; }