Skip to content
TORNLIFE More

Torn Error to HTTP Status Code Snippet (Python)

Started by JotDe [2200962] on in API Development.

3 replies · 123 views · thread synced · 3 days ago · View on torn.com
About this thread

Posts archived: 4 / 4 posts (100%) · the total is Torn's reply count + the opening post at the last fetch

Counted by TornLife from the archived posts.

Archived posts
4
Discussion span
→
People posting
2
Likes on archived posts
2
Authority score
65 / 100
Historical score
17 / 100
Story score
26 / 100
Engagement score
45 / 100
JotDe [2200962]
For a while now, I've been bothered by the way the API reports errors.
So I thought about how a general mapping function could look like to encapsulate the design flaw.
I think in other dynamic languages the mapping can be used almost the same way.

If they ever change the API to the http standard, the function is robust and will not break.

I had to remove the source, because the forum does not preserve the spaces.
Here is the working fiddle.

Just replace the 2 value in the env-vars with your api-key.
Helcostr [1934501] Wiki Editor

if code in [1, 2, 3, 4, 6]:
response.status_code = 400
elif code in [0, 12]:
response.status_code = 500
elif code in [7, 8, 10, 11]:
response.status_code = 403
elif code in [9]:
response.status_code = 503
elif code in [5]:
response.status_code = 429

This is all you really need, just convert it to your language of choice (basically modifying the statuscode of the response, assuming that the reponse can be modified before it's passed to the interpreter... otherwise one would need to find an alternative route).

You can even take it one step further and throw status codes, so that a catcher can deal with the status code errors (away from your main logic)
JotDe [2200962]
response.raise_for_status() throws/raises an HTTPError.
This is the nice thing in this example. You don't have to care if it is a clouflare error or a torn error. :)