Errors

    Error in errors pacakge implements GRPCStatus() interface,The conversion between grpc and HTTP error code is realized, and the business reason is returned through errorinfo.

    To generate code with protoc.

    1. protoc --proto_path=. \
    2. --proto_path=./third_party \
    3. --go_out=paths=source_relative:. \
    4. --go-errors_out=paths=source_relative:. \
    5. $(API_PROTO_FILES)

    After successful execution, will be generated in the api/helloworld directory a go file,The code is as follows.

    1. package helloworld
    2. import (
    3. fmt "fmt"
    4. )
    5. // is compatible with the kratos package it is being compiled against.
    6. const _ = errors.SupportPackageIsVersion1
    7. func IsUserNotFound(err error) bool {
    8. if err == nil {
    9. return false
    10. }
    11. e := errors.FromError(err)
    12. return e.Reason == ErrorReason_USER_NOT_FOUND.String() && e.Code == 404
    13. }
    14. }
    15. func IsContentMissing(err error) bool {
    16. if err == nil {
    17. return false
    18. }
    19. e := errors.FromError(err)
    20. return e.Reason == ErrorReason_CONTENT_MISSING.String() && e.Code == 400
    21. }
    22. func ErrorContentMissing(format string, args ...interface{}) *errors.Error {
    23. return errors.New(400, ErrorReason_CONTENT_MISSING.String(), fmt.Sprintf(format, args...))