Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions errors/codes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package errors

import (
"fmt"
"github.com/golang/protobuf/proto"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

type CodeErr int

type CodeErrEntity struct {
Code string
Status codes.Code
Message string
}

const (
ErrGeneral CodeErr = iota
)

var codeErrMap = map[CodeErr]CodeErrEntity{
ErrGeneral: {Code: "ERR999", Status: codes.InvalidArgument, Message: "Terjadi kesalahan."},
}

func GetCodeErrMap[T CodeErrEntity](k CodeErr) T {
return T(codeErrMap[k])
}

func (c CodeErr) GRPCStatus() *status.Status {
return status.New(codeErrMap[c].Status, c.Error())
}

func (c CodeErr) Error() string {
codeErrEntity := codeErrMap[c]
return fmt.Sprintf("[%s] %s", codeErrEntity.Code, codeErrEntity.Message)
}

type CodeErrEntityWithDetails struct {
codeErr CodeErr
Details []proto.Message
}

func NewCodeErrEntityWithDetails(codeErr CodeErr, details ...proto.Message) *CodeErrEntityWithDetails {
return &CodeErrEntityWithDetails{codeErr: codeErr, Details: details}
}

func (c CodeErrEntityWithDetails) Error() string {
return c.codeErr.Error()
}

func (c CodeErrEntityWithDetails) GRPCStatus() *status.Status {
grpcStatus := c.codeErr.GRPCStatus()
if withDetails, err := grpcStatus.WithDetails(c.Details...); err == nil {
return withDetails
}
return grpcStatus
}
58 changes: 58 additions & 0 deletions errors/error.handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package errors

import (
"context"
"encoding/json"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc/status"
"net/http"
"regexp"
)

func ErrorHandlerFunc(ctx context.Context, _ *runtime.ServeMux, _ runtime.Marshaler, w http.ResponseWriter, req *http.Request, err error) {
code, message, details, httpStatus := getErrorCode(err)

// Customize the error response
customError := map[string]interface{}{
"code": code,
"message": message,
}

if details != nil {
customError["details"] = details
}

// Set the response header
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(httpStatus)

// Write the response
if err = json.NewEncoder(w).Encode(customError); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

func getErrorCode(err error) (code string, message string, details interface{}, httpStatus int) {
// Convert the error to a gRPC status
grpcStatus, ok := status.FromError(err)
if !ok {
return "ERR999", "Unknown error", nil, http.StatusInternalServerError
}

s := grpcStatus.Message()
re := regexp.MustCompile(`^\[(.*)\]\s+(.*)$`)
matches := re.FindAllStringSubmatch(s, -1)

if len(matches) == 1 && len(matches[0]) == 3 {
var d interface{}
d = grpcStatus.Details()
grpcDetails := grpcStatus.Details()
if len(grpcDetails) < 2 {
d = grpcDetails[0]
}

return matches[0][1], matches[0][2], d, runtime.HTTPStatusFromCode(grpcStatus.Code())
}

return "ERR999", s, nil, http.StatusInternalServerError
}
34 changes: 21 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@ module github.com/harryosmar/codegen-go
go 1.20

require (
github.com/grpc-ecosystem/grpc-gateway/v2 v2.6.0
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4
google.golang.org/grpc v1.53.0
google.golang.org/protobuf v1.30.0
github.com/golang/protobuf v1.5.4
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0
google.golang.org/genproto/googleapis/api v0.0.0-20240711142825-46eb208f015d
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.2
)

require (
github.com/ghodss/yaml v1.0.0 // indirect
github.com/golang/glog v1.1.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/grpc-ecosystem/grpc-gateway v1.16.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.3.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto v0.0.0-20240711142825-46eb208f015d // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240711142825-46eb208f015d // indirect
)
Loading