Skip to content

Commit 6756dfd

Browse files
committed
fix api error on invalid token
1 parent e152bba commit 6756dfd

2 files changed

Lines changed: 39 additions & 7 deletions

File tree

spec/System/TestTradeQueryRequests_spec.lua

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,42 @@ describe("TradeQueryRequests", function()
6565
launch = orig_launch
6666
end)
6767

68+
-- Pass: Does not crash on 401, and passes error message
69+
-- Fail: Crash, or returned error is wrong
70+
it("does not crash on 401", function()
71+
local json = '"{"error":"invalid_token","error_description":"The access token provided is invalid or has expired"}"'
72+
local header = [[HTTP/1.1 401 Unauthorized
73+
Date: Fri, 24 Apr 2026 07:30:38 GMT
74+
Content-Type: application/json
75+
Transfer-Encoding: chunked
76+
Connection: keep-alive
77+
Server: cloudflare
78+
WWW-Authenticate: Bearer realm="pathofexile:production", error="invalid_token", error_description="The access token provided is invalid or has expired"
79+
Cache-Control: no-store
80+
Strict-Transport-Security: max-age=63115200; includeSubDomains; preload]]
81+
local orig_launch = launch
82+
launch = {
83+
DownloadPage = function(url, onComplete, opts)
84+
onComplete({ body = json, header = header }, nil)
85+
end
86+
}
87+
table.insert(requests.requestQueue.search, {
88+
url = "test",
89+
callback = function(body, msg)
90+
assert.are.equal(body, json)
91+
assert.truthy(msg:find("Response code: 401"))
92+
end,
93+
retryTime = nil
94+
})
95+
local function mock_next_time(self, policy, time)
96+
return time - 1
97+
end
98+
mock_limiter.NextRequestTime = mock_next_time
99+
requests:ProcessQueue()
100+
assert.are.equal(#requests.requestQueue.search, 0)
101+
launch = orig_launch
102+
end)
103+
68104
-- Pass: Retries with increasing backoff up to cap, preventing infinite loops
69105
-- Fail: No backoff or uncapped, indicating retry bug, risking API bans
70106
it("retries on 429 with exponential backoff", function()

src/Classes/TradeQueryRequests.lua

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,9 @@ function TradeQueryRequestsClass:ProcessQueue(onRateLimit)
5454
end
5555
return
5656
end
57-
-- if limit rules don't return account then the auth token is invalid.
58-
if response.header:match("[xX]%-[rR]ate%-[lL]imit%-[rR]ules: (.-)\n"):match("Account") == nil and main.api.authToken then
59-
if errMsg then
60-
errMsg = errMsg .. "\nAuthorization is invalid. Please Re-Log and reset"
61-
else
62-
errMsg = "Authorization is invalid. Please Re-Log and reset"
63-
end
57+
if errMsg == "Response code: 401" and response.body:find("invalid_token") then
58+
errMsg = errMsg .. "\nAuthorization is invalid. Please Re-Log and reset"
59+
main.api:ResetDetails()
6460
end
6561
request.callback(response.body, errMsg, unpack(request.callbackParams or {}))
6662
end

0 commit comments

Comments
 (0)