2023-07-07 14:49:44 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ErrorReason uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
Internal = iota
|
|
|
|
Unavailable
|
|
|
|
BadRequest
|
2023-12-07 17:37:11 +00:00
|
|
|
NotFound
|
2023-07-07 14:49:44 +00:00
|
|
|
// Add more errors as needed
|
|
|
|
)
|
|
|
|
|
|
|
|
type RpcError struct {
|
|
|
|
Err error
|
|
|
|
Reason ErrorReason
|
|
|
|
}
|
|
|
|
|
|
|
|
func ErrorReasonToGRPC(reason ErrorReason) codes.Code {
|
|
|
|
switch reason {
|
|
|
|
case Internal:
|
|
|
|
return codes.Internal
|
|
|
|
case Unavailable:
|
|
|
|
return codes.Unavailable
|
|
|
|
case BadRequest:
|
|
|
|
return codes.InvalidArgument
|
2023-12-07 17:37:11 +00:00
|
|
|
case NotFound:
|
|
|
|
return codes.NotFound
|
2023-07-07 14:49:44 +00:00
|
|
|
// Add more cases for other error reasons as needed
|
|
|
|
default:
|
|
|
|
return codes.Internal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ErrorReasonToHTTP(reason ErrorReason) int {
|
|
|
|
switch reason {
|
|
|
|
case Internal:
|
|
|
|
return http.StatusInternalServerError
|
|
|
|
case Unavailable:
|
|
|
|
return http.StatusServiceUnavailable
|
|
|
|
case BadRequest:
|
|
|
|
return http.StatusBadRequest
|
2023-12-07 17:37:11 +00:00
|
|
|
case NotFound:
|
|
|
|
return http.StatusNotFound
|
2023-07-07 14:49:44 +00:00
|
|
|
// Add more cases for other error reasons as needed
|
|
|
|
default:
|
|
|
|
return http.StatusInternalServerError
|
|
|
|
}
|
|
|
|
}
|