From 7acc4ebbd6bea0746ecfcacae0b66d979165a765 Mon Sep 17 00:00:00 2001 From: DeniskaRediska Date: Wed, 20 Nov 2019 17:28:50 +0300 Subject: [PATCH] update error message --- authenticate/authenticate.go | 20 ++++++++++---------- authenticate/error.go | 9 +++++++-- handler/handler.go | 4 +++- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/authenticate/authenticate.go b/authenticate/authenticate.go index d068f8e..fd62917 100644 --- a/authenticate/authenticate.go +++ b/authenticate/authenticate.go @@ -43,7 +43,7 @@ func Do(ctx *fasthttp.RequestCtx) (int32, error) { pathStr := getPathWithoutPrefix(path) if _, ok := routing.AllMethods[pathStr]; !ok { - return 0, createError(codes.Unimplemented) + return 0, createError("not implemented", codes.Unimplemented) } for _, notExpectedHeader := range notExpectedHeaders { @@ -57,29 +57,29 @@ func Do(ctx *fasthttp.RequestCtx) (int32, error) { appId int32 = -1 ) if len(appToken) == 0 { - return 0, createError(codes.Unauthenticated) + return 0, createError("unauthorized", codes.Unauthenticated) } else if config.GetRemote().(*conf.RemoteConfig).TokensSetting.ApplicationVerify { if appId, err = verifyToken.Application(string(appToken)); err != nil || appId == 0 { - return 0, createError(codes.Unauthenticated, "invalid token") + return 0, createError("unauthorized", codes.Unauthenticated, "invalid token") } } verifiableKeys, err := auth.verify.ApplicationToken(string(appToken)) if err != nil { log.Error(log_code.ErrorAuthenticate, err) - return 0, createError(codes.Internal) + return 0, createError("internal Server error", codes.Internal) } if len(verifiableKeys) != 4 { - return 0, createError(codes.Unauthenticated, "unknown token") + return 0, createError("unauthorized", codes.Unauthenticated, "unknown token") } applicationId, err := strconv.Atoi(verifiableKeys[utils.ApplicationIdHeader]) if err != nil { log.Error(log_code.ErrorAuthenticate, errors.WithMessagef(err, "parse appId from redis")) - return 0, createError(codes.Internal) + return 0, createError("internal Server error", codes.Internal) } if appId != -1 && int32(applicationId) != appId { - return 0, createError(codes.Unauthenticated, "unknown application identity") + return 0, createError("unauthorized", codes.Unauthenticated, "unknown application identity") } verifiableKeys[utils.DeviceTokenHeader] = string(ctx.Request.Header.Peek(utils.DeviceTokenHeader)) @@ -89,10 +89,10 @@ func Do(ctx *fasthttp.RequestCtx) (int32, error) { verifiableKeys, permittedToCall, err = auth.verify.Identity(verifiableKeys, pathStr) if err != nil { log.Error(log_code.ErrorAuthenticate, err) - return 0, createError(codes.Internal) + return 0, createError("internal Server error", codes.Internal) } if permittedToCall { - return 0, createError(codes.PermissionDenied) + return 0, createError("forbidden", codes.PermissionDenied) } for key, value := range verifiableKeys { @@ -104,7 +104,7 @@ func Do(ctx *fasthttp.RequestCtx) (int32, error) { if _, ok := routing.InnerMethods[pathStr]; ok { adminToken := ctx.Request.Header.Peek("x-auth-admin") //todo const key if verifyToken.Admin(string(adminToken)) != nil { - return 0, createError(codes.PermissionDenied) + return 0, createError("forbidden", codes.PermissionDenied) } } diff --git a/authenticate/error.go b/authenticate/error.go index d17399d..65a59ac 100644 --- a/authenticate/error.go +++ b/authenticate/error.go @@ -5,11 +5,12 @@ import ( "google.golang.org/grpc/codes" ) -func createError(status codes.Code, details ...interface{}) error { - return ErrorDescription{grpcStatus: status, details: details} +func createError(msg string, status codes.Code, details ...interface{}) error { + return ErrorDescription{message: msg, grpcStatus: status, details: details} } type ErrorDescription struct { + message string grpcStatus codes.Code details []interface{} } @@ -18,6 +19,10 @@ func (e ErrorDescription) Error() string { return fmt.Sprintf("%v", e) } +func (e ErrorDescription) Message() string { + return e.message +} + func (e ErrorDescription) ConvertToGrpcStatus() codes.Code { return e.grpcStatus } diff --git a/handler/handler.go b/handler/handler.go index 2bf4c99..840b9c9 100644 --- a/handler/handler.go +++ b/handler/handler.go @@ -66,14 +66,16 @@ func (handlerHelper) AuthenticateAccountingProxy(ctx *fasthttp.RequestCtx) domai applicationId, err := authenticate.Do(ctx) if err != nil { + message := "unknown error" status := codes.Unknown details := make([]interface{}, 0) switch e := err.(type) { case authenticate.ErrorDescription: + message = e.Message() status = e.ConvertToGrpcStatus() details = e.Details() } - utils.WriteError(ctx, "unauthorized", status, details) + utils.WriteError(ctx, message, status, details) return domain.Create().SetError(err) }