35 lines
828 B
Go
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
|
|
}
|