pkgdash/handler/mapping.go
Vasiliy Tolstov 2ef12956ac initial rewrite, use micro v4 and not cms-xxx stuff
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2023-08-11 21:27:38 +03:00

35 lines
828 B
Go

package handler
import (
"context"
"errors"
"net/http"
pb "go.unistack.org/unistack-org/pkgdash/proto"
)
const (
internalErrorCode = "1"
badRequestCode = "2"
notFoundErrorCode = "3"
)
func mapError(ctx context.Context, err error) (result *pb.Error, status int) {
status = http.StatusBadRequest
switch errors.Unwrap(err).(type) {
case *UnmarshalError:
result = &pb.Error{Code: badRequestCode, Title: "Bad request"}
case *ParametersMissingError:
result = &pb.Error{Code: badRequestCode, Title: "Required parameters are missing"}
case *NotFoundError:
result = &pb.Error{Code: notFoundErrorCode, Title: "Not found"}
status = http.StatusNotFound
default:
status = http.StatusInternalServerError
result = &pb.Error{Code: internalErrorCode, Title: "Internal error", Details: err.Error()}
}
return
}