当前位置:网站首页>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 !
边栏推荐
- [digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]
- LeetCode102. Sequence traversal of binary tree (output by layer and unified output)
- Hainan Nuanshen tea recruits warmhearted people: recruitment of the product experience recommender of Nuanshen multi bubble honey orchid single cluster
- Alibaba Tianchi SQL training camp task4 learning notes
- Summary of binary tree recursive routines
- Getting started stm32--gpio (running lantern) (nanny level)
- 2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
- 实现反向代理客户端IP透传
- 2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
- 2.13 summary
猜你喜欢

TypeError: this. getOptions is not a function

MoCo: Momentum Contrast for Unsupervised Visual Representation Learning

Go language implementation principle -- lock implementation principle

YML configuration, binding and injection, verification, unit of bean

【原创】程序员团队管理的核心是什么?

Masked Autoencoders Are Scalable Vision Learners (MAE)

Negative sampling

Registration of Electrical Engineering (elementary) examination in 2022 and the latest analysis of Electrical Engineering (elementary)

Mathematical formula screenshot recognition artifact mathpix unlimited use tutorial

Simple and beautiful method of PPT color matching
随机推荐
Development specification: interface unified return value format [resend]
【原创】程序员团队管理的核心是什么?
Leetcode weekly The 280 game of the week is still difficult for the special game of the week's beauty team ~ simple simulation + hash parity count + sorting simulation traversal
Leecode learning notes
2022 R2 mobile pressure vessel filling review simulation examination and R2 mobile pressure vessel filling examination questions
openresty ngx_lua正則錶達式
C Primer Plus Chapter 9 question 9 POW function
Judge whether the binary tree is a complete binary tree
It is proved that POJ 1014 module is optimized and pruned, and some recursion is wrong
Data type, variable declaration, global variable and i/o mapping of PLC programming basis (CoDeSys)
Douban scoring applet Part-2
From the perspective of quantitative genetics, why do you get the bride price when you get married
Non rigid / flexible point cloud ICP registration
数学公式截图识别神器Mathpix无限使用教程
Hcip day 11 (BGP agreement)
Media query: importing resources
Getting started stm32--gpio (running lantern) (nanny level)
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]
Alibaba Tianchi SQL training camp task4 learning notes
Go语言实现原理——Map实现原理