当前位置:网站首页>How to design API return code (error code)?
How to design API return code (error code)?
2022-07-05 23:18:00 【Java technology stack】
Click on the official account ,Java dried food Timely delivery 
source :https://ken.io/note/api-errorcode-or-resultcode-desgin
One 、 Preface
Two 、HTTP Status code reference
With HTTP Take the status code as an example , In order to more clearly express and distinguish the meaning of status code ,HTTP The state is segmented .
| piecewise | Segment description |
|---|---|
| 1XX | Information , The server receives the request , Requester is required to continue |
| 2XX | success , Operation received and processed successfully |
| 3XX | Redirect , Further action is required to complete the request |
| 4XX | Client error , The request contains a syntax error or could not be completed |
| 5XX | Server error , The server encountered an error while processing the request |
For back-end development , What we usually see is :
2XX Status code , such as 200-> The request is successful ,
5XX Status code , such as 502-> Server exception , Usually, the service is not running properly , Or code execution error
The cause of the problem can be preliminarily determined through the status code ,HTTP The design idea of state is worth learning .
3、 ... and 、 Parameter conventions
Although it is a return code design , But only code You can't , There should also be corresponding message, So that people can understand
| Field | type | explain |
|---|---|---|
| code | int | Return code |
| message | string | Return code description |
Reference resources HTTP The idea of status code , We segment the error code
| Return code value | explain |
|---|---|
| 0 | success |
| 99999 | An unknown exception occurred in the system |
| 10000-19999 | Parameter check error |
| 20000-29999 | A Step execution failed |
| 30000-39999 | B Step execution failed |
Through this design , It is very convenient to distinguish between programs and people API Return result of , The key is unity ! Want to be an architect , This The architect's Atlas suggests taking a look , Little detours .
Four 、 Individualization Message
Usually ours message It's all for Engineers , But in different scenarios , The same mistake , Users may need to see different error prompts . Click on the official account ,Java dried food Timely delivery 
For example 20000-29999 Indicates that the order creation failed :
| application_id | code | message |
|---|---|---|
| 100001 | 20001 | I'm sorry , You have an order in progress , Please go to my order list to process . |
| 100001 | 20002 | I'm sorry , You have an order in progress , Please go to my order list to process . |
Then when the request processing is finished and is about to return , according to application_id+code, To match and replace message.

So we can make the phone APP Users of 、 Users of wechat applet 、 Business users who place orders on the web page see different messages . The latest interview questions have been sorted out , You can Java Interview library applet online brush questions .
5、 ... and 、 Unified processing of return information
With a unified code, We can go through Nginx perhaps APM Tool Statistics API request Code Quantity and distribution information .
We can according to the unit time 99999 To do sth API Abnormal alarm of
We can use Code Return pie chart , Help us discover the system 、 Problems in business processes
wait , All in all , Good return code design , Can help us improve communication efficiency , Reduce the maintenance cost of the code .


Good start ! Reissue 10,000 Red envelope cover
2021 What happened in 10 It's a technical event !!
23 Design mode and Practice ( Very comprehensive )
replace Log4j2!tinylog Born in the sky
Based on a Spring Boot The fairy's private life project
Explode !Java Xie Cheng is coming !
Weigh on Guan Xuan :Redis Here comes the object mapping framework !!
Recommend a code artifact , Save at least half the amount of code !
Spring Boot 3.0 M1 Release , Formal abandonment Java 8
Spring Boot Learning notes , This is so complete !
Focus on Java Technology stack, see more dry goods


obtain Spring Boot Practical notes !
边栏推荐
- C Primer Plus Chapter 9 question 9 POW function
- Use of grpc interceptor
- [screen recording] how to record in the OBS area
- Getting started stm32--gpio (running lantern) (nanny level)
- 进击的技术er——自动化
- [untitled]
- LabVIEW打开PNG 图像正常而 Photoshop打开得到全黑的图像
- Leetcode sword finger offer brush questions - day 21
- Southeast Asia e-commerce guide, how do sellers layout the Southeast Asia market?
- Simple and beautiful method of PPT color matching
猜你喜欢

如何快速理解复杂业务,系统思考问题?

Hcip day 11 (BGP agreement)

One article deals with the microstructure and instructions of class
![[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]](/img/df/9aa83ac5bd9f614942310a040a6dff.jpg)
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]

Vision Transformer (ViT)

Detailed explanation of pointer and array written test of C language

Hcip day 12 (BGP black hole, anti ring, configuration)

2022 registration examination for safety management personnel of hazardous chemical business units and simulated reexamination examination for safety management personnel of hazardous chemical busines

Element operation and element waiting in Web Automation

Simple and beautiful method of PPT color matching
随机推荐
leecode-学习笔记
TVS管和ESD管的技术指标和选型指南-嘉立创推荐
Basic knowledge of database (interview)
Multi sensor fusion of imu/ electronic compass / wheel encoder (Kalman filter)
Leetcode sword finger offer brush questions - day 21
Shell: operator
Leetcode buys and sells stocks
6-axis and 9-axis IMU attitude estimation
LabVIEW打开PNG 图像正常而 Photoshop打开得到全黑的图像
Thoroughly understand JVM class loading subsystem
openresty ngx_lua请求响应
Hj16 shopping list
TypeError: this. getOptions is not a function
2022 registration examination for safety management personnel of hazardous chemical business units and simulated reexamination examination for safety management personnel of hazardous chemical busines
Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
Use of shell:for loop
Openresty ngx Lua regular expression
Multi view 3D reconstruction
Development specification: interface unified return value format [resend]
The maximum happiness of the party