From 19787f85cadb6bf2d050ff092cf26c6e92aa8df5 Mon Sep 17 00:00:00 2001 From: Adphi Date: Wed, 8 Mar 2023 11:44:25 +0100 Subject: [PATCH] errors: add BadRequestDetails Signed-off-by: Adphi --- errors/details.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 errors/details.go diff --git a/errors/details.go b/errors/details.go new file mode 100644 index 0000000..395d4a7 --- /dev/null +++ b/errors/details.go @@ -0,0 +1,20 @@ +package errors + +import ( + "google.golang.org/genproto/googleapis/rpc/errdetails" +) + +// BadRequestDetails returns an error details for an invalid argument. +// fd is a list of field / description pairs. +func BadRequestDetails(fd ...string) *errdetails.BadRequest { + var fieldViolations []*errdetails.BadRequest_FieldViolation + for i := 0; i < len(fd); i += 2 { + fieldViolations = append(fieldViolations, &errdetails.BadRequest_FieldViolation{ + Field: fd[i], + Description: fd[i+1], + }) + } + return &errdetails.BadRequest{ + FieldViolations: fieldViolations, + } +}