From 4ff83c574424cb382bb71cd1f38d05b8360ba5aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Thu, 18 Jul 2019 19:48:24 +0200 Subject: [PATCH] Return errors in Permissioned API --- api/permissioned.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/api/permissioned.go b/api/permissioned.go index 4a5ec3a99..591f08155 100644 --- a/api/permissioned.go +++ b/api/permissioned.go @@ -2,8 +2,9 @@ package api import ( "context" - "fmt" "reflect" + + "golang.org/x/xerrors" ) type permKey int @@ -50,8 +51,17 @@ func Permissioned(a API) API { } } - // TODO: return as error - panic(fmt.Sprintf("unauthorized call to %s", field.Name)) + err := xerrors.Errorf("missing permission to invoke '%s' (need '%s')", field.Name, requiredPerm) + rerr := reflect.ValueOf(&err).Elem() + + if field.Type.NumOut() == 2 { + return []reflect.Value{ + reflect.Zero(field.Type.Out(0)), + rerr, + } + } else { + return []reflect.Value{rerr} + } })) }