Restful Api
API 错误处理的最佳实践 这篇文章认为,API 错误处理的目标不仅是告诉客户端“请求失败了”,更重要的是帮助客户端理解问题并采取正确的处理方式。作者围绕这一观点,从服务端和客户端两个角度总结错误处理的设计原则,重点讨论如何设计一致且易于理解的错误响应,包括 HTTP 状态码的使用、统一的错误响应结构、错误信息的表达、日志与可观测性等内容,并进一步介绍这些原则在 REST、GraphQL 和 gRPC 中的具体实践。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 { "status": "error", // This can be ‘error’ or ‘success’ "statusCode": 404, "error": { "code": "RESOURCE_NOT_FOUND", "message": "The requested resource was not found.", "details": "The user with the ID '12345' does not exist in our records.", "timestamp": "2023-12-08T12:30:45Z", "path": "/api/v1/users/12345", "suggestion": "Please check if the user ID is correct or refer to our documentation at https://api.example.com/docs/errors#RESOURCE_NOT_FOUND for more information." }, "requestId": "a1b2c3d4-e5f6-7890-g1h2-i3j4k5l6m7n8", "documentation_url": "https://api.example.com/docs/errors" } 防抖 和 节流 的定义。 ...