Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/impl structured error #1320

Merged
merged 32 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
e195609
feat: impl structured error
abyssparanoia Nov 21, 2024
96627a3
feat: impl structured error
abyssparanoia Nov 21, 2024
c3328b0
feat: impl structured error
abyssparanoia Nov 21, 2024
16fdad3
feat: impl structured error
abyssparanoia Nov 21, 2024
55ae9ac
feat: impl structured error
abyssparanoia Nov 21, 2024
eca3a7d
feat: impl structured error
abyssparanoia Nov 21, 2024
bd21888
feat: impl structured error
abyssparanoia Nov 21, 2024
5abaa9c
feat: impl structured error
abyssparanoia Nov 21, 2024
813388a
feat: impl structured error
abyssparanoia Nov 21, 2024
d4e7a96
feat: impl structured error
abyssparanoia Nov 21, 2024
f12ac58
feat: impl structured error
abyssparanoia Nov 21, 2024
2ff8908
feat: impl structured error
abyssparanoia Nov 21, 2024
9e78f49
feat: impl structured error
abyssparanoia Nov 21, 2024
ca90a3d
feat: impl structured error
abyssparanoia Nov 21, 2024
95c6c69
feat: impl structured error
abyssparanoia Nov 21, 2024
dfdf60f
feat: impl structured error
abyssparanoia Nov 21, 2024
74b8085
feat: impl structured error
abyssparanoia Nov 21, 2024
5278d35
feat: impl structured error
abyssparanoia Nov 21, 2024
26d7882
feat: impl structured error
abyssparanoia Nov 21, 2024
644f643
feat: impl structured error
abyssparanoia Nov 21, 2024
692df64
feat: impl structured error
abyssparanoia Nov 21, 2024
6106439
feat: impl structured error
abyssparanoia Nov 21, 2024
710eb10
feat: impl structured error
abyssparanoia Nov 21, 2024
f00a302
feat: impl structured error
abyssparanoia Nov 21, 2024
35975a7
feat: impl structured error
abyssparanoia Nov 21, 2024
a3fec48
feat: impl structured error
abyssparanoia Nov 21, 2024
a37eb6a
feat: impl structured error
abyssparanoia Nov 21, 2024
f0db880
feat: impl structured error
abyssparanoia Nov 21, 2024
49104a0
feat: impl structured error
abyssparanoia Nov 21, 2024
13067be
feat: impl structured error
abyssparanoia Nov 21, 2024
37b49f2
fix: lint
abyssparanoia Nov 21, 2024
4a2f5a3
feat: add forbid rule for zap.Error
abyssparanoia Nov 21, 2024
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
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ linters-settings:
excludes:
- G115

forbidigo:
forbid:
- fmt\.Print.*
- zap\.Error.*

linters:
disable-all: true
enable:
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ lint.go:
@go run github.com/golangci/golangci-lint/cmd/golangci-lint run
$(call format)

.PHONY: lint.go.fix
lint.go.fix:
@go run github.com/golangci/golangci-lint/cmd/golangci-lint run --fix
$(call format)

.PHONY: lint.proto
lint.proto:
@go run github.com/bufbuild/buf/cmd/buf lint
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
cloud.google.com/go/spanner v1.73.0
cloud.google.com/go/storage v1.47.0
firebase.google.com/go v3.13.0+incompatible
github.com/abyssparanoia/goerr v0.7.0
github.com/abyssparanoia/memeduck v1.0.3
github.com/air-verse/air v1.61.1
github.com/avast/retry-go v3.0.0+incompatible
Expand Down Expand Up @@ -208,6 +209,7 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a // indirect
github.com/golangci/go-printf-func-name v0.1.0 // indirect
github.com/golangci/gofmt v0.0.0-20240816233607-d8596aa466a9 // indirect
github.com/golangci/misspell v0.6.0 // indirect
github.com/golangci/modinfo v0.3.4 // indirect
Expand Down Expand Up @@ -238,7 +240,6 @@ require (
github.com/jgautheron/goconst v1.7.1 // indirect
github.com/jingyugao/rowserrcheck v1.1.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jirfag/go-printf-func-name v0.0.0-20200119135958-7558a9eaa5af // indirect
github.com/jjti/go-spancheck v0.6.2 // indirect
github.com/julz/importas v0.1.0 // indirect
github.com/karamaru-alpha/copyloopvar v1.1.0 // indirect
Expand All @@ -260,7 +261,6 @@ require (
github.com/lestrrat-go/httprc v1.0.6 // indirect
github.com/lestrrat-go/iter v1.0.2 // indirect
github.com/lestrrat-go/option v1.0.1 // indirect
github.com/lufeee/execinquery v1.2.1 // indirect
github.com/macabu/inamedparam v0.1.3 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/maratori/testableexamples v1.0.0 // indirect
Expand Down Expand Up @@ -314,7 +314,9 @@ require (
github.com/quasilyte/stdinfo v0.0.0-20220114132959-f7386bf02567 // indirect
github.com/quic-go/qpack v0.5.1 // indirect
github.com/quic-go/quic-go v0.48.1 // indirect
github.com/raeperd/recvcheck v0.1.2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.13.1 // indirect
github.com/rs/cors v1.11.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/ryancurrah/gomodguard v1.3.5 // indirect
Expand Down Expand Up @@ -355,6 +357,7 @@ require (
github.com/ultraware/funlen v0.1.0 // indirect
github.com/ultraware/whitespace v0.1.1 // indirect
github.com/uudashr/gocognit v1.1.3 // indirect
github.com/uudashr/iface v1.2.0 // indirect
github.com/vbatts/tar-split v0.11.6 // indirect
github.com/volatiletech/inflect v0.0.1 // indirect
github.com/volatiletech/randomize v0.0.1 // indirect
Expand Down
172 changes: 49 additions & 123 deletions go.sum

Large diffs are not rendered by default.

49 changes: 0 additions & 49 deletions internal/domain/errors/bad_request_error.go

This file was deleted.

52 changes: 52 additions & 0 deletions internal/domain/errors/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//nolint:govet
package errors

import "github.com/abyssparanoia/goerr"

func NewBadRequestError(errCode string, msg string) *goerr.Error {
return goerr.New(msg).
WithCategory(ErrorCategoryBadRequest.String()).
WithCode(errCode)
}

func NewUnauthorizedError(errCode string, msg string) *goerr.Error {
return goerr.New(msg).
WithCategory(ErrorCategoryUnauthorized.String()).
WithCode(errCode)
}

func NewForbiddenError(errCode string, msg string) *goerr.Error {
return goerr.New(msg).
WithCategory(ErrorCategoryForbidden.String()).
WithCode(errCode)
}

func NewNotFoundError(errCode string, msg string) *goerr.Error {
return goerr.New(msg).
WithCategory(ErrorCategoryNotFound.String()).
WithCode(errCode)
}

func NewConflictError(errCode string, msg string) *goerr.Error {
return goerr.New(msg).
WithCategory(ErrorCategoryConflict.String()).
WithCode(errCode)
}

func NewCanceledError(errCode string, msg string) *goerr.Error {
return goerr.New(msg).
WithCategory(ErrorCategoryCanceled.String()).
WithCode(errCode)
}

func NewInternalError(errCode string, msg string) *goerr.Error {
return goerr.New(msg).
WithCategory(ErrorCategoryInternal.String()).
WithCode(errCode)
}

func NewServiceAvailableError(errCode string, msg string) *goerr.Error {
return goerr.New(msg).
WithCategory(ErrorCategoryServiceAvailable.String()).
WithCode(errCode)
}
49 changes: 0 additions & 49 deletions internal/domain/errors/canceled_error.go

This file was deleted.

49 changes: 0 additions & 49 deletions internal/domain/errors/conflict_error.go

This file was deleted.

19 changes: 19 additions & 0 deletions internal/domain/errors/error_category.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package errors

type ErrorCategory string

const (
ErrorCategoryUnknown ErrorCategory = "unknown"
ErrorCategoryBadRequest ErrorCategory = "bad_request"
ErrorCategoryUnauthorized ErrorCategory = "unauthorized"
ErrorCategoryForbidden ErrorCategory = "forbidden"
ErrorCategoryNotFound ErrorCategory = "not_found"
ErrorCategoryConflict ErrorCategory = "conflict"
ErrorCategoryCanceled ErrorCategory = "canceled"
ErrorCategoryInternal ErrorCategory = "internal"
ErrorCategoryServiceAvailable ErrorCategory = "service_available"
)

func (c ErrorCategory) String() string {
return string(c)
}
66 changes: 11 additions & 55 deletions internal/domain/errors/errors.go
Original file line number Diff line number Diff line change
@@ -1,62 +1,18 @@
package errors

import "github.com/pkg/errors"

const (
// common error.
InternalErr InternalError = "E100001"
RequestInvalidArgumentErr BadRequestError = "E100002"
InvalidIDTokenErr UnauthorizedError = "E100003"
RequireStaffSessionErr UnauthorizedError = "E100004"
InvalidAdminRequestUserErr UnauthorizedError = "E100005"
AssetInvalidErr BadRequestError = "E100006"
AssetNotFoundErr NotFoundError = "E100007"
CanceledErr CanceledError = "E900001"
var (
InternalErr = NewInternalError("E100001", "An internal error has occurred")
RequestInvalidArgumentErr = NewBadRequestError("E100002", "Request argument is invalid")
InvalidIDTokenErr = NewUnauthorizedError("E100003", "Invalid ID token")
RequireStaffSessionErr = NewUnauthorizedError("E100004", "Require staff session")
InvalidAdminRequestUserErr = NewUnauthorizedError("E100005", "Invalid admin request user")
AssetInvalidErr = NewBadRequestError("E100006", "Asset is invalid")
AssetNotFoundErr = NewNotFoundError("E100007", "Asset not found")
CanceledErr = NewCanceledError("E100008", "Canceled")

// tenant error.
TenantNotFoundErr NotFoundError = "E200101"
TenantNotFoundErr = NewNotFoundError("E200101", "Tenant not found")

// staff error.
StaffNotFoundErr NotFoundError = "E200201"
StaffNotFoundErr = NewNotFoundError("E200201", "Staff not found")
)

var errorMessageMap = map[error]string{
// common error
InternalErr: "An internal error has occurred",
RequestInvalidArgumentErr: "Request argument is invalid",
InvalidIDTokenErr: "Invalid ID token",
RequireStaffSessionErr: "Require staff session",
AssetInvalidErr: "Asset is invalid",
InvalidAdminRequestUserErr: "Invalid admin request user",

// tenant error
TenantNotFoundErr: "Tenant not found",

// staff error
StaffNotFoundErr: "Staff not found",
}

func ExtractPlaneErrMessage(err error) (code string, message string) { //nolint: nonamedreturns
switch errors.Cause(err) {
// tenant error
case TenantNotFoundErr:
return TenantNotFoundErr.Error(), errorMessageMap[TenantNotFoundErr]

// staff error
case StaffNotFoundErr:
return StaffNotFoundErr.Error(), errorMessageMap[StaffNotFoundErr]

// common error
case InvalidIDTokenErr:
return InvalidIDTokenErr.Error(), errorMessageMap[InvalidIDTokenErr]
case RequireStaffSessionErr:
return RequireStaffSessionErr.Error(), errorMessageMap[RequireStaffSessionErr]
case InvalidAdminRequestUserErr:
return InvalidAdminRequestUserErr.Error(), errorMessageMap[InvalidAdminRequestUserErr]
case AssetInvalidErr:
return AssetInvalidErr.Error(), errorMessageMap[AssetInvalidErr]
case RequestInvalidArgumentErr:
return RequestInvalidArgumentErr.Error(), errorMessageMap[RequestInvalidArgumentErr]
}
return InternalErr.Error(), errorMessageMap[InternalErr]
}
Loading