Error Codes
About 2 mincomponenterrcodehttpgrpc
Core Features of the Error Code
Services created by sponge provide a comprehensive error code system, supporting both HTTP and gRPC protocols, with the following core features:
Dual Protocol Support
- Simultaneously supports HTTP and gRPC error code systems
- Each protocol includes system-level and business-level error codes
Standard Protocol Conversion
- HTTP system-level error codes are converted to standard HTTP status codes as needed
- gRPC system-level error codes are converted to standard gRPC status codes as needed
Advanced Features
- Supports custom error message overwriting
- Supports intelligent mapping from gRPC error codes to HTTP error codes
Error Code Specification
Error Code Structure
The error code consists of 6 digits, the first digit indicates the error type, the second to fourth digits indicate the error module or table number, and the fifth to sixth digits indicate the API serial number, as shown in the following table:
First digit | Middle three digits | Last two digits |
---|---|---|
1 is http system-level error2 is http business-level error3 is grpc system-level error4 is grpc system-level error | Table or module number, range 1~999 | API serial number, range 1~99 |
Service Type | System-level Error Code Range | Business-level Error Code Range |
---|---|---|
http | 100000 ~ 200000 | 200000 ~ 300000 |
grpc | 300000 ~ 400000 | 400000 ~ 500000 |
Example of HTTP Error Code Usage
Create web server based on SQL
, use the following error code:
import "github.com/go-dev-frame/sponge/pkg/gin/response"
// return error, the standard http status code is always 200
response.Error(c, ecode.InvalidParams)
// rewrite error messages, the standard http status code is always 200
response.Error(c, ecode.InvalidParams.RewriteMsg("custom error message"))
// convert error code to standard http status code
response.Out(c, ecode.InvalidParams)
// convert error code to standard http status code, and rewrite error messages
response.Out(c, ecode.InvalidParams.RewriteMsg("custom error message"))
Create web server based on Protobuf
, use the following error code:
// return error, the standard http status code is always 200
return nil, ecode.InvalidParams.Err()
// rewrite error messages, the standard http status code is always 200
return nil, ecode.InvalidParams.Err("custom error message")
// convert error code to standard http status code
return nil, ecode.InvalidParams.ErrToHTTP()
// convert error code to standard http status code, and rewrite error messages
return nil, ecode.InvalidParams.ErrToHTTP("custom error message")
Example of gRPC Error Code Usage
Create gRPC server based on SQL/Protobuf
, use the following error code:
// return error
return nil, ecode.StatusInvalidParams.Err()
// rewrite error messages
return nil, ecode.StatusInvalidParams.Err("custom error message")
// convert error code to standard grpc status code
return nil, ecode.StatusInvalidParams.ToRPCErr()
// convert error code to standard grpc status code, and rewrite error messages
return nil, ecode.StatusInvalidParams.ToRPCErr("custom error message")
Example of error code used when calling gRPC service at gRPC gateway:
// convert error code to standard http status code
return nil, ecode.StatusInvalidParams.ErrToHTTP()
// convert error code to standard http status code, and rewrite error messages
return nil, ecode.StatusInvalidParams.ErrToHTTP("custom error message")