alanvardy/tod

Better API / Reqwest error handling - Parse/Return error_tag with failures (and/or verbose)

Closed this issue · 3 comments

If task creation or similar fails, the Todoist API returns error HTTP code 403

However, the specific actual reason is returned in the error_tag, for example:

 ~ curl https://api.todoist.com/sync/v9/quick/add \
>     -H "Authorization: Bearer 0123456789abcdef0123456789abcdef01234567" \
>     -d text='Buy Milk @Drinks #ShoppingList +Bob'
{"error":"Invalid token","error_code":401,"error_extra":{"access_type":"access_token","event_id":"5395d5e61a0349a6a4057d4696cbbaa4","retry_after":4},"error_tag":"AUTH_INVALID_TOKEN","http_code":403}%

or

➜  ~ curl https://api.todoist.com/sync/v9/quick/add \
>     -H "Authorization: Bearer xxxxx" \
>     -d text='Buy Milk @Drinks #ShoppingList +Bob'
{"error":"Maximum number of items per user project limit reached","error_code":49,"error_extra":{"event_id":"979ed76032f54af58c859bc0193e6d9f","retry_after":5},"error_tag":"MAX_ITEMS_LIMIT_REACHED","http_code":403}%

Currently simply the error is returned from request on an API failure, for example, like this:

Error from reqwest:

            method: POST
            url: /sync/v9/quick/add
            body: {"auto_reminder":true,"text":"test"}
            Error: Response { url: Url { scheme: "https", cannot_be_a_base: false, username: "", password: None, host: Some(Domain("api.todoist.com")), port: None, path: "/sync/v9/quick/add", query: None, fragment: None }, status: 403, headers: {"content-type": "application/json", "content-length": "214", "connection": "keep-alive", "date": "Thu, 13 Jun 2024 19:33:46 GMT", "cache-control": "no-cache", "referrer-policy": "strict-origin-when-cross-origin", "retry-after": "4", "server": "gunicorn", "set-cookie": "tduser=v4.public.eyJ1c2VyX2lkIjogMjIxMTUwNSwgImV4cCI6ICIyMDI0LTA2LTI3VDE5OjMzOjQ2KzAwOjAwIn0pD4h0um_hDVXL5UWBXzUJmztvoaLWWhqoJP365ACAaxtHf9jV1R4fm8KtK4QTjOdDnU2q8tktD27CcF_F-70I; Domain=.todoist.com; Expires=Thu, 27-Jun-2024 19:33:46 GMT; Max-Age=1209600; Secure; HttpOnly; Path=/", "set-cookie": "todoistd=\"/CUdA09psYiwY7pwgn9sRGC/RQQ=?\"; Domain=.todoist.com; Expires=Fri, 13-Jun-2025 19:33:46 GMT; Max-Age=31536000; Secure; HttpOnly; Path=/; SameSite=None", "strict-transport-security": "max-age=31536000; includeSubDomains; preload", "vary": "Accept-Encoding", "x-cache": "Error from cloudfront", "via": "1.1 2df6c48373521343b51b753e9a855d62.cloudfront.net (CloudFront)", "x-amz-cf-pop": "SLC52-P1", "x-amz-cf-id": "BfW9-1hxA0IQYVrNX-V_jFcdkRvJZ2bNHFJ7utkCh7yvk-qVC4kCnA=="} }

Instead, the error_tag should be parsed and returned on an error failure, i.e. "Auth_INVALID_TOKEN" or "MAX_ITEMS_LIMIT_REACHED" - as it details the actual reason Todoist API is failing on the request.

It probably makes sense to not even return any of the Reqwest error handling unless in Verbose.

Yes, this is great.

Got something better now:

image

Addressed in this PR: #806