Urgent.News

What's breaking now, across hundreds of outlets.

Tech

遇到API调用问题?常见错误代码与排查手册

引言 在开发过程中,API 调用是连接前后端、第三方服务与业务逻辑的桥梁。然而,接口报错几乎是每位开发者都会遇到的日常挑战。面对形形色色的错误代码,如果缺乏系统的排查思路,往往会在定位问题上耗费大量时间。本文整理了一份常见 API 错误代码的排查手册,帮助你快速识别问题根源,并给出对应的解决思路。 错误代码分类概览 API 错误代码通常遵循 HTTP 状态码的语义,但不同服务商也会在此基础上扩展自定义错误码。整体上,我们可以将常见错误分为以下几类: 4xx 客户端错误:请求本身存在问题,例如参数错误、鉴权失败、资源不存在等。 5xx 服务端错误:服务端处理异常,例如内部错误、网关超时、服务不可用等。 自定义业务错误码:服务商在 HTTP 状态码之外定义的业务级错误,用于更精确地描述业务逻辑问题。 常见 4xx 错误代码与排查 3.1 400 Bad Request…

Original Chinese Read in English

API calls often present challenges for developers. This article presents a handbook for diagnosing common API error codes to help quickly identify the root cause and find appropriate solutions.

API error codes typically follow HTTP status codes, with some service providers extending them with custom error codes. Common errors can be categorized into four groups: client errors (4xx), server errors (5xx), and custom business error codes.

Common client errors and their troubleshooting steps include:

1. 400 Bad Request: Indicates request syntax errors or parameter issues. Check for:

- Complete and correct parameters

- Correct parameter types (string, number, boolean)

- Valid JSON/XML request body format

- Parameter value ranges or format constraints (e.g., date format, enum values)

2. 401 Unauthorized: Requests not authenticated. Check for:

- Missing or expired Access Token/API Key

- Incorrect Token format (e.g., missing Bearer prefix)

- Authorization header name mismatch

3. 403 Forbidden: Authentication passed, but permissions denied. Check:

- Enabled permissions for the API

- Correct API Key binding to application/project

- Exceeded rate limits or quota limits

4. 404 Not Found: Requested resource or path does not exist. Check:

- Exact URL match with documentation

- Correct resource ID

- Correct API version number

Common server errors and troubleshooting steps include:

5. 500 Internal Server Error: Indicates unexpected server issues. Check:

- Server logs for specific exception stacks

- Parameter triggering boundary conditions or null pointer issues

- Status of dependent components like databases and caches

6. 502 Bad Gateway: Gateway or proxy unable to get valid response from upstream service. Common causes include:

- Upstream service crash or not started

- Load balancer configuration error

- Upstream service timeout causing gateway disconnection

7. 503 Service Unavailable: Service currently unavailable, often due to overload or maintenance. Check:

- Service status page or announcements for maintenance windows

- Server load conditions due to sudden traffic spikes

- Increased retry intervals to avoid overloading the server

Custom business error codes typically include additional information in the response body. To troubleshoot these:

- Refer to the service provider's official error code documentation

- Use the message or description fields in the response body for specific details

- Understand the relationship between business codes and HTTP status codes

- Establish an error code mapping table in your code for unified handling and logging

A universal troubleshooting process involves:

1. Reproducing the issue with recorded request and response details

2. Checking the official documentation for request format and authentication

3. Examining network connectivity, DNS resolution, and proxy settings

4. Reviewing server logs for any anomalies

5. Comparing environment configurations between test and production

Utilizing tools like Postman, curl, or browser developer tools can aid in debugging. Properly structured error handling practices, including request encapsulation, comprehensive logging, and monitoring, can help prevent API issues from occurring in the first place.

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

I Turned My GitHub Profile Into a Dark Souls-Inspired RPG Character Sheet ⚔️

What if your GitHub profile looked less like a developer résumé and more like a character screen from Dark Souls? That was the idea behind my latest GitHub profile redesign.

  • GitHub profile redesigned as Dark Souls character sheet
  • RPG-style health and stamina display for technical skills
  • Custom SVG banners, bonfire visuals, and animated contributions

A container in 81 lines of Bash: what the kernel primitives cost on Ubuntu 24.04

Early in this lab I had a shell that was PID 1, and from that same shell ps listed systemd as PID 1. Both were right. The kernel had put the shell in a new PID namespace; ps was still reading a /proc…

  • Container built using only kernel primitives in Bash script for Ubuntu 24.04
  • Six isolation checks performed: hostname, filesystem, memory limits, network, namespaces, cleanup
  • Alpine minirootfs 3.24.2 used, container processes isolated from host processes

More from Monday 12 October →