35 lines
857 B
Go
35 lines
857 B
Go
|
package handler
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
pb "go.unistack.org/unistack-org/pkgdash/proto/go_generate"
|
||
|
"net/http"
|
||
|
|
||
|
"golang.org/x/net/context"
|
||
|
)
|
||
|
|
||
|
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
|
||
|
}
|