当前位置: 首页 > 工具软件 > Kratos > 使用案例 >

kratos的返回值问题与错误返回问题

萧晔
2023-12-01

习惯了这种格式的返回值:

{
	"code":"0",
	"message":"成功"
    "captchaId": "0TFqCFHqeGuvYFy3EajN",
    ...
}

今天按照kratos写api发现返回的是这种

{
    "captchaId": "0TFqCFHqeGuvYFy3EajN",
    "captcha": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPAAAABQCAMAAAAQlwhOAAAA81BMVEUAAAAmLVKIj7QKETYHDjOWncIECzAvNluAh6ynrtM0O2CaocZ6gaZPVnt9hKllbJEuNVoYH0QpMFUECzCjqs8sM1hVXIETGj97gqdDSm+XnsMNFDkRGD19hKkCCS4KETYOFTpzep9vdptOVXo7QmeqsdaDiq8AByxrcpdudZodJEkZIEUZIEWPlrssM1hxeJ1QV3x4f6QcI0ior9QlLFF6gaYKETaFjLFDSm9eZYo6QWYfJkt9hKmaocZaYYaDiq+Yn8Sjqs8gJ0woL1R7gqeVnMGRmL1YX4SLkrdbYoctNFmPlrsBCC0mLVIvNltOVXqZoMUuqQojAAAAAXRSTlMAQObYZgAABItJREFUeJzsnHtLIz0UxhNUttJCKVWk7qjUtYJI/1EEq1YUlFpZv//XeZlMZnKZ2zlJTmb03Ydltk5zOT+fTK6tjELHJKX2V8fH34P4KlhJ4Xlfg5fI2NUVhPgPQc3ten2tI04Ye3QtFcT7pyPimvtJkjw+OhND1A1vvXwc9tGii0oD66Tl/TP1crJYLBj7TR0RrU5OmonPzgriyWSS8v6mIv5FVO7G/BHjsLiS8f6iId5sNvDEf0lCqJHFuxOqXAzv36jERnPb2QlGjFAN7wVRdUaHQsJ77pTr4oKMmKjcXOfnjsTBI2F7YYq5aXnfjZdAe3tBiG9u2oh7o0gO6zoEpOlNiwigw8N2Ytdn3lmkC4YeOmwsCalmnr2Szks086zWS8S66hSV9yUwMeTx6VQvYZ9eSAepxBnjPL3GVOD+GcWbwvLs6l3xFzhlIy9iAeYgzgUuDwD99QUnbhBqiY2VS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FTkSuQmCC"
}

可以在http中添加方法,统一添加业务成功代码

package server

import (
	"github.com/go-kratos/kratos/v2/encoding"
	"github.com/go-kratos/kratos/v2/log"
	"github.com/go-kratos/kratos/v2/middleware/logging"
	mmd "github.com/go-kratos/kratos/v2/middleware/metadata"
	"github.com/go-kratos/kratos/v2/middleware/metrics"
	"github.com/go-kratos/kratos/v2/middleware/recovery"
	"github.com/go-kratos/kratos/v2/middleware/validate"
	"github.com/go-kratos/kratos/v2/transport/http"
	"github.com/go-kratos/swagger-api/openapiv2"
	baseHttp "kratosadmin/api/base/v1"
	userHttp "kratosadmin/api/user/v1"
	"kratosadmin/app/user/service/internal/conf"
	"kratosadmin/app/user/service/internal/service"
	"kratosadmin/app/user/service/middleware"
	httpNet "net/http"
	"time"
)

// NewHTTPServer new a HTTP server.
func NewHTTPServer(c *conf.Server, userService *service.UserService, baseService *service.BaseService, logger log.Logger, middleware2 middleware.Middleware) *http.Server {
	var opts = []http.ServerOption{
		http.Middleware(
			recovery.Recovery(),
			logging.Server(logger),
			metrics.Server(),
			validate.Validator(),
			mmd.Server(),
			middleware2.Auth(),
		),
		http.Filter(middleware2.Cors()),
	}
	if c.Http.Network != "" {
		opts = append(opts, http.Network(c.Http.Network))
	}
	if c.Http.Addr != "" {
		opts = append(opts, http.Address(c.Http.Addr))
	}

	if c.Http.Timeout != nil {
		opts = append(opts, http.Timeout(c.Http.Timeout.AsDuration()))
	}
	opts = append(opts, http.ResponseEncoder(func(
		w httpNet.ResponseWriter,
		r *httpNet.Request,
		i interface{},
	) error {
		type response struct {
			Code int
			Data interface{}
			Ts   string
		}
		reply := &response{
			Code: 200,
			Data: i,
			Ts:   time.Now().String(),
		}
		codec := encoding.GetCodec("json")
		data, err := codec.Marshal(reply)
		if err != nil {
			return err
		}
		w.Header().Set("Content-Type", "application/json")
		w.Write(data)
		return nil
	}))

	srv := http.NewServer(opts...)

	//user服务
	userHttp.RegisterUserHTTPServer(srv, userService)
	//基础服务
	baseHttp.RegisterBaseHTTPServer(srv, baseService)
	openAPIhandler := openapiv2.NewHandler()
	srv.HandlePrefix("/q/", openAPIhandler)
	return srv
}

返回值就变成了下面的了,复合要求
资料来源:https://mp.weixin.qq.com/s/4ocdoAVXXKTvJ3U65YXltw

{
    "Code": 200,
    "Data": {
        "captchaId": "NYAa3oSuojuvzBFvCX7u",
        "captcha": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAPAAAABQCAMAAAAQlwhOAAAA81BMVEUAAAAKBUZEP4B+ebp3crMwK2xGQYKQi8wjHl8cF1gTDk8YE1QfGlsjHl97dreIg8QhHF2wq+xFQIFMR4iVkNGind5YU5QPCksdGFl4c7RcV5iLhsddWJmGgcJ4c7STjs9TTo9eWZp4c7SBfL0uKWq9uPkPCkurpucdGFlMR4hkX6CDfr8/OnunouO9uPk/OnuHgsMwK2wqJWZGQYKoo+SAe7w9OHl/ertoY6RjXp8fGltqZaafmttLRoeMh8h2cbI9OHmOicq9uPmWkdIfGluUj9Bwa6yKhcYzLm9GQYITDk8JBEVPSotHQoOuqeqmoeJ2cbLVIGdPAAAAAXRSTlMAQObYZgAABL5JREFUeJzsnPtL40oUx2ewv4j4g7IKqRiq0tU2WK0IgqAsFFq01P//z7k0ySTzPmdeSbp3vxfummQe55Mz75mU/FOprG8DOlaW/e+I+zbgn/a6BEOcdmJHV7q8lInvpevT07+MWLq+v+eJV3+dh1UJvKtVj5Y46StOMoPhfQWef32JxNdJrUmv11eQWLi6vj54Ysfwg+F9BJ6/w0kcx7KlCz0+2onf30Hi42MNcRFkVUql8XBRDJc4jYbOO3T7YithCVymSjhMVt5FQMLL5UCJLVosgoidQk8DctLpTLiiyFgLch7ZjlI/6q3p9CVa8tme94wnphSLfH6egPjnR0P88hKLOMuyqeLhvVDQaF6XIqnhJQTilRcHzMqmU8maErZCZtBonxuk5BFb4uIAIA0vY66pyz+ClJjXxcOqKOVgKb58H6rCPUxbJbQznioPE4L08J3h/q5Oa/jQFS53ZTX77k4kZr7d7XZ1rAN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+xsixqcFyXacxqqov9UA8EZVogNTwfovAAD//5NXQwNE75qgAAAAAElFTkSuQmCC"
    },
    "Ts": "2021-09-22 14:48:21.9387671 +0800 CST m=+0.239897501"
}

然后,貌似还有一种哲学,既然正常返回http状态吗为200,那么返回体内容就应该是业务数据不应该再加载业务code/message等,所以kratos默认返回的符合这个逻辑,那么错误消息怎么返回呢?比如400错误、500错误呢,为什么这么设计,业务code与http code一致呢?可以方便运维抓取接口状态数据吧
链接地址:https://go-kratos.dev/docs/component/errors

可以这样自定义错误

// 通过 errors.New() 响应错误
	err = errors.New(500, "USER_NAME_EMPTY", "user name is empty")

也可以这样:

// 通过 proto 生成的代码响应错误,并且包名应替换为自己生成代码后的 package name
	err = v1.ErrorUsernameConflict("user %s not found", "kratos")

覆盖返回错误码即可,比如这样返回结果

{
    "code": 409,
    "reason": "USERNAME_CONFLICT",
    "message": "user kratos not found",
    "metadata": {}
}
 类似资料: