From 8fb8c62190c7edf43aa5bb3e3668ffc81bde393a Mon Sep 17 00:00:00 2001 From: Naohito-T Date: Sun, 21 Apr 2024 13:58:45 +0900 Subject: [PATCH 1/2] =?UTF-8?q?:recycle:=20Fix:=20custom=20error=E3=82=92i?= =?UTF-8?q?s=E3=81=A7=E5=88=86=E5=B2=90=E3=81=A7=E3=81=8D=E3=82=8B?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E3=81=AA=E3=81=A3=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 8 --- backend/.go-version | 2 +- backend/.golangci.yml | 6 +++ backend/Makefile | 4 +- backend/build/.keep | 0 backend/configs/router.go | 14 +++++ backend/{deployments => docker}/Dockerfile | 0 .../{build => docker}/localstack/init-aws.sh | 0 backend/docker/scripts/tool | 4 ++ backend/domain/customerror/errors.go | 54 +++++++++++++++++++ backend/domain/errors.go | 13 ----- backend/go.mod | 10 ++-- backend/go.sum | 1 - backend/internal/rest/handler/health.go | 16 ++++++ backend/internal/rest/handler/shorturl.go | 11 ++++ .../internal/rest/middleware/error/error.go | 30 ++++++----- .../internal/rest/middleware/middleware.go | 18 +++++-- .../rest/middleware/security/security.go | 11 ++++ backend/internal/rest/router/router.go | 14 +++-- backend/staticcheck.conf | 9 ---- docker-compose.yml | 4 +- 21 files changed, 167 insertions(+), 62 deletions(-) delete mode 100644 backend/build/.keep create mode 100644 backend/configs/router.go rename backend/{deployments => docker}/Dockerfile (100%) rename backend/{build => docker}/localstack/init-aws.sh (100%) create mode 100644 backend/docker/scripts/tool create mode 100644 backend/domain/customerror/errors.go delete mode 100644 backend/domain/errors.go create mode 100644 backend/internal/rest/handler/health.go create mode 100644 backend/internal/rest/handler/shorturl.go create mode 100644 backend/internal/rest/middleware/security/security.go delete mode 100644 backend/staticcheck.conf diff --git a/.vscode/settings.json b/.vscode/settings.json index 778a75c..4f983c7 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,8 +2,6 @@ "editor.formatOnSave": true, "go.formatTool": "goimports", "go.lintTool": "golangci-lint", - // workspaceFolderはVisual Studio Code (VS Code)の変数で、現在開いているワークスペースのルートフォルダを指します。 - // つまり、この変数はVS Codeで開かれているプロジェクトの最上位ディレクトリのパスに解決されます。 "go.lintFlags": [ "--config=${workspaceFolder}/backend/.golangci.yml", "--fast" @@ -14,12 +12,6 @@ "source.organizeImports": "explicit" } }, - "[go.mod]": { - "editor.formatOnSave": true, - "editor.codeActionsOnSave": { - "source.organizeImports": "explicit" - } - }, "markdownlint.config": { "MD013": false, "MD033": false diff --git a/backend/.go-version b/backend/.go-version index 2844977..3500250 100644 --- a/backend/.go-version +++ b/backend/.go-version @@ -1 +1 @@ -1.21.1 +1.21.0 diff --git a/backend/.golangci.yml b/backend/.golangci.yml index 72fc90f..7512bcb 100644 --- a/backend/.golangci.yml +++ b/backend/.golangci.yml @@ -1,6 +1,12 @@ +# see: https://golangci-lint.run/ +# reference: https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml run: deadline: 5m +issues: + # golangci-lintがdefaultで無効にしているlintersを有効にする + exclude-use-default: false +# linters-settings: linters: disable-all: true enable: diff --git a/backend/Makefile b/backend/Makefile index 9c573a8..37859f8 100644 --- a/backend/Makefile +++ b/backend/Makefile @@ -8,7 +8,7 @@ test: # see https://logmi.jp/tech/articles/324549 # セキュリティチェック sec: - gosec ./... + golangci-lint run --enable=gosec ./... # エラーチェック errcheck: @@ -16,7 +16,7 @@ errcheck: # 静的解析 staticcheck: - staticcheck ./... + golangci-lint run --enable=staticcheck ./... # フォーマットとインポートの整理 format: diff --git a/backend/build/.keep b/backend/build/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/backend/configs/router.go b/backend/configs/router.go new file mode 100644 index 0000000..c0f12b7 --- /dev/null +++ b/backend/configs/router.go @@ -0,0 +1,14 @@ +package configs + +// type MyType intと宣言するDefined type +// 以前はNamed typeと言っていたが、Go1.11からDefined typeと呼ぶようになった +// type MyType = intと宣言するType alias +// see: https://zenn.dev/bluetree/articles/9d52842dff35cc +// go 1.19から導入された型エイリアス(これで推測され、string()などのキャストが不要になる) +type path = string + +const ( + Health path = "/health" + GetShortURL path = "/api/v1/urls/:shortUrl" + CreateShortURL path = "/api/v1/urls" +) diff --git a/backend/deployments/Dockerfile b/backend/docker/Dockerfile similarity index 100% rename from backend/deployments/Dockerfile rename to backend/docker/Dockerfile diff --git a/backend/build/localstack/init-aws.sh b/backend/docker/localstack/init-aws.sh similarity index 100% rename from backend/build/localstack/init-aws.sh rename to backend/docker/localstack/init-aws.sh diff --git a/backend/docker/scripts/tool b/backend/docker/scripts/tool new file mode 100644 index 0000000..bb3bb54 --- /dev/null +++ b/backend/docker/scripts/tool @@ -0,0 +1,4 @@ +#!/bin/bash + +# 将来的には開発ツールもdocker内で実行するようにする。 +docker run -t --rm -v $(pwd):/app -w /app golangci/golangci-lint:v1.57.2 golangci-lint run -v diff --git a/backend/domain/customerror/errors.go b/backend/domain/customerror/errors.go new file mode 100644 index 0000000..a4db177 --- /dev/null +++ b/backend/domain/customerror/errors.go @@ -0,0 +1,54 @@ +package customerror + +import ( + "fmt" + "net/http" + "net/url" +) + +// ApplicationError はアプリケーション特有のエラー情報を保持する基本構造体です。 +type ApplicationError struct { + Code int + Message string +} + +// Error メソッドは error インターフェースを実装します。 +func (e *ApplicationError) Error() string { + return fmt.Sprintf("Code: %d, Message: %s", e.Code, e.Message) +} + +// WrongEmailVerificationError は特定の条件下で利用されるカスタムエラーです。 +type WrongEmailVerificationError struct { + ApplicationError + IsTally bool + IsRedirect bool + RedirectQuery *url.Values +} + +// NewWrongEmailVerificationError は WrongEmailVerificationError を生成します。 +func NewWrongEmailVerificationError(isTally, isRedirect bool, redirectQuery string) *WrongEmailVerificationError { + q, _ := url.ParseQuery(redirectQuery) + return &WrongEmailVerificationError{ + ApplicationError: ApplicationError{ + Code: http.StatusBadRequest, + Message: "Wrong email verification code", + }, + IsTally: isTally, + IsRedirect: isRedirect, + RedirectQuery: &q, + } +} + +// LogError はエラーのロギングを行うメソッドです。 +func (e *WrongEmailVerificationError) LogError(req *http.Request) { + fmt.Println("Error occurred:", e.Error(), "Query:", e.RedirectQuery.Encode()) +} + +// Is メソッドを追加 +// func (e *WrongEmailVerificationError) Is(target error) bool { +// t, ok := target.(*WrongEmailVerificationError) +// return ok && e.Code == t.Code +// } + +// WrongEmailVerificationErrorInstance は WrongEmailVerificationError のグローバルインスタンスです。 +var WrongEmailVerificationErrorInstance = NewWrongEmailVerificationError(false, false, "") diff --git a/backend/domain/errors.go b/backend/domain/errors.go deleted file mode 100644 index dee6238..0000000 --- a/backend/domain/errors.go +++ /dev/null @@ -1,13 +0,0 @@ -package domain - -import "errors" - -var ( - ErrInternalServerError = errors.New("internal server error") - - ErrNotFound = errors.New("your requested Item is not found") - - ErrConflict = errors.New("your Item already exist") - - ErrBadParamInput = errors.New("given Param is not valid") -) diff --git a/backend/go.mod b/backend/go.mod index 9fb51fc..5294a49 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -1,18 +1,17 @@ module github.com/naohito-T/tinyurl/backend -go 1.21.1 +go 1.21 require ( + github.com/aws/aws-sdk-go-v2 v1.26.1 github.com/aws/aws-sdk-go-v2/config v1.27.11 + github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.13.13 github.com/aws/aws-sdk-go-v2/service/dynamodb v1.31.1 github.com/labstack/echo/v4 v4.11.4 - github.com/stretchr/testify v1.8.4 ) require ( - github.com/aws/aws-sdk-go-v2 v1.26.1 // indirect github.com/aws/aws-sdk-go-v2/credentials v1.17.11 // indirect - github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.13.13 // indirect github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1 // indirect github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5 // indirect github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5 // indirect @@ -25,13 +24,11 @@ require ( github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.4 // indirect github.com/aws/aws-sdk-go-v2/service/sts v1.28.6 // indirect github.com/aws/smithy-go v1.20.2 // indirect - github.com/davecgh/go-spew v1.1.1 // indirect github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect github.com/labstack/gommon v0.4.2 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect golang.org/x/crypto v0.17.0 // indirect @@ -39,5 +36,4 @@ require ( golang.org/x/sys v0.15.0 // indirect golang.org/x/text v0.14.0 // indirect golang.org/x/time v0.5.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/backend/go.sum b/backend/go.sum index d94e06b..984d2c6 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -71,7 +71,6 @@ golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/time v0.5.0 h1:o7cqy6amK/52YcAKIPlM3a+Fpj35zvRj2TP+e1xFSfk= golang.org/x/time v0.5.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/backend/internal/rest/handler/health.go b/backend/internal/rest/handler/health.go new file mode 100644 index 0000000..136bd5e --- /dev/null +++ b/backend/internal/rest/handler/health.go @@ -0,0 +1,16 @@ +package handler + +import ( + "net/http" + + "github.com/labstack/echo/v4" + "github.com/naohito-T/tinyurl/backend/domain/customerror" +) + +func HealthHandler(c echo.Context) error { + a := true + if a { + return customerror.WrongEmailVerificationErrorInstance + } + return c.JSON(http.StatusOK, map[string]string{"message": "ok2"}) +} diff --git a/backend/internal/rest/handler/shorturl.go b/backend/internal/rest/handler/shorturl.go new file mode 100644 index 0000000..b146d42 --- /dev/null +++ b/backend/internal/rest/handler/shorturl.go @@ -0,0 +1,11 @@ +package handler + +import ( + "net/http" + + "github.com/labstack/echo/v4" +) + +func ShortURLHandler(c echo.Context) error { + return c.String(http.StatusOK, "OK") +} diff --git a/backend/internal/rest/middleware/error/error.go b/backend/internal/rest/middleware/error/error.go index 79ac941..0848899 100644 --- a/backend/internal/rest/middleware/error/error.go +++ b/backend/internal/rest/middleware/error/error.go @@ -1,30 +1,36 @@ package error +// see: https://go.dev/play/p/TzZE1mdL63_1 + import ( + "errors" "net/http" "github.com/labstack/echo/v4" + "github.com/naohito-T/tinyurl/backend/domain/customerror" ) +// これはGo言語における型アサーション(type assertion)の一例です。型アサーションは、インターフェースの値が特定の型を持っているかどうかをチェックし、その型の値を取り出すために使用されます。 +// このコードの場合、err はエラーを表すインターフェース型です。*echo.HTTPError は echo パッケージの HTTPError 型のポインタです。この行は、err が *echo.HTTPError 型の値を持っているかをチェックし、もしそうであれば he にその値を割り当て、ok に true を設定します。err が *echo.HTTPError 型でなければ、ok は false になり、he は nil になります。 +// この型アサーションを使うことで、安全に型変換を行いつつ、エラーチェックを同時に実施できます。 func CustomErrorHandler(err error, c echo.Context) { + c.Logger().Error("カスタムエラーに入ったよ") + if errors.Is(err, customerror.WrongEmailVerificationErrorInstance) { + c.Logger().Error("これがWrongEmailVerificationErrorアプリケーションエラー") + } he, ok := err.(*echo.HTTPError) if !ok { he = echo.NewHTTPError(http.StatusInternalServerError, err.Error()) } - // ロギング c.Logger().Error(err) - // エラータイプによって条件分岐 - if he.Code == http.StatusNotFound { - // リダイレクトを行う例 - if err := c.Redirect(http.StatusTemporaryRedirect, "/some-fallback-url"); err != nil { - c.Logger().Error(err) - } - } else { - // エラーレスポンスを送信する例 - if err := c.JSON(he.Code, map[string]string{"message": he.Message.(string)}); err != nil { - c.Logger().Error(err) - } + // エラーレスポンスを送信する例 + if err := c.JSON(he.Code, map[string]string{"message": he.Message.(string)}); err != nil { + c.Logger().Error(err) } } + +// func buildError(err error, code int) *echo.HTTPError { + +// } diff --git a/backend/internal/rest/middleware/middleware.go b/backend/internal/rest/middleware/middleware.go index 2f9e85e..1995270 100644 --- a/backend/internal/rest/middleware/middleware.go +++ b/backend/internal/rest/middleware/middleware.go @@ -1,14 +1,24 @@ package middleware +// see: https://echo.labstack.com/docs/category/middleware + import ( "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" - "github.com/naohito-T/tinyurl/backend/internal/rest/middleware/accesslog" - "github.com/naohito-T/tinyurl/backend/internal/rest/middleware/error" + + // "github.com/naohito-T/tinyurl/backend/internal/rest/middleware/accesslog" + ehandler "github.com/naohito-T/tinyurl/backend/internal/rest/middleware/error" ) +// loggerの考え方 +// https://yuya-hirooka.hatenablog.com/entry/2021/10/15/123607 func CustomMiddleware(e *echo.Echo) { - e.Use(accesslog.AccessLog()) - e.HTTPErrorHandler = error.CustomErrorHandler + // e.Use(accesslog.AccessLog()) + // expect this handler is used as fallback unless a more specific is present e.Use(middleware.Recover()) + // これでechoのloggerを操作できる。 + // e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{ + // Format: "time=${time_rfc3339_nano}, method=${method}, uri=${uri}, status=${status}\n", + // })) + e.HTTPErrorHandler = ehandler.CustomErrorHandler } diff --git a/backend/internal/rest/middleware/security/security.go b/backend/internal/rest/middleware/security/security.go new file mode 100644 index 0000000..70b9192 --- /dev/null +++ b/backend/internal/rest/middleware/security/security.go @@ -0,0 +1,11 @@ +package security + +import ( + "github.com/labstack/echo/v4" +) + +func AttachSecurity(e *echo.Echo) { + // サーバー起動時のバナーを非表示にする + e.HideBanner = true + e.HidePort = true +} diff --git a/backend/internal/rest/router/router.go b/backend/internal/rest/router/router.go index c9c9461..abb6b82 100644 --- a/backend/internal/rest/router/router.go +++ b/backend/internal/rest/router/router.go @@ -5,18 +5,26 @@ import ( "net/http" "github.com/labstack/echo/v4" + Router "github.com/naohito-T/tinyurl/backend/configs" "github.com/naohito-T/tinyurl/backend/internal/infrastructures/slog" + "github.com/naohito-T/tinyurl/backend/internal/rest/handler" // "github.com/naohito-T/tinyurl/backend/internal/rest/container" ) +func catchAllHandler(c echo.Context) error { + return c.JSON(http.StatusNotFound, map[string]string{"message": "Route not found"}) +} + // https://tinyurl.com/app/api/url/create" // NewRouter これもシングルトンにした場合の例が気になる func NewRouter(e *echo.Echo) { // container := container.NewGuestContainer() + e.GET(Router.Health, handler.HealthHandler) + e.GET(Router.GetShortURL, hello) + e.POST(Router.CreateShortURL, hello) - e.GET("/health", hello) - e.GET("/api/v1/urls/:shortUrl", hello) - e.POST("/api/v1/urls", hello) + // 未定義のルート用のキャッチオールハンドラ + e.Any("/*", catchAllHandler) } func hello(c echo.Context) error { diff --git a/backend/staticcheck.conf b/backend/staticcheck.conf deleted file mode 100644 index 61511c9..0000000 --- a/backend/staticcheck.conf +++ /dev/null @@ -1,9 +0,0 @@ -# https://staticcheck.dev/docs/checks -# checksという配列の中に有効にしたいチェックの番号を記述します。 -# inheritでデフォルトで有効なチェックを全て指定することができます。 -# 基本的には、上記のように「inherit + デフォルトで無効だけど有効にしたいチェック」のような書き方になると思います。 -# SA staticcheck コードの正しさに関するチェック -# S simple コードの簡潔さに関するチェック -# ST stylecheck コーディングスタイルに関するチェック -# QF quickfix gopls の自動リファクタリングとして表示されるチェック -checks = ["inherit", "ST1003", "ST1016", "ST1020", "ST1021", "ST1022"] diff --git a/docker-compose.yml b/docker-compose.yml index 177d1ee..1eb7e24 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,13 +9,13 @@ services: DEFAULT_REGION: ap-northeast-1 volumes: # ready hook - - "$PWD/backend/build/localstack/init-aws.sh:/etc/localstack/init/ready.d/init-aws.sh" + - "$PWD/backend/docker/localstack/init-aws.sh:/etc/localstack/init/ready.d/init-aws.sh" - 'localstack-data:/tmp/localstack' backend: build: context: $PWD/backend - dockerfile: $PWD/backend/deployments/Dockerfile + dockerfile: $PWD/backend/docker/Dockerfile entrypoint: "air -c .air.toml" environment: AWS_ACCESS_KEY_ID: "XXXX" From b909b7a2340a45a8fccf423f33dde298a0d81521 Mon Sep 17 00:00:00 2001 From: Naohito-T Date: Sun, 21 Apr 2024 15:02:37 +0900 Subject: [PATCH 2/2] :recycle: Fix: error handler --- backend/domain/customerror/code.go | 29 ++++++++++++++++++ backend/domain/customerror/errors.go | 13 +++++--- .../internal/rest/middleware/error/error.go | 30 +++++++++---------- 3 files changed, 52 insertions(+), 20 deletions(-) create mode 100644 backend/domain/customerror/code.go diff --git a/backend/domain/customerror/code.go b/backend/domain/customerror/code.go new file mode 100644 index 0000000..47dea41 --- /dev/null +++ b/backend/domain/customerror/code.go @@ -0,0 +1,29 @@ +package customerror + +// ApplicationError はアプリケーション特有のエラー情報を保持する基本構造体です。 +type ApplicationErrorCode struct { + // StatusCode(JSON parseでは構造体省略) + Status int `json:"-"` + // Code はエラーコードを表します。 + Code string `json:"code"` + // Message はエラーメッセージを表します。 + Message string `json:"message"` +} + +var ( + WrongEmailVerificationCode ApplicationErrorCode = ApplicationErrorCode{ + Code: "WRONG_EMAIL_VERIFICATION_ERROR", + Message: "Wrong email verification code", + } + + // Unknown Error: システムがエラーの原因を特定できないか、エラーの種類が分類されていない場合に使用する。 + UnknownCode ApplicationErrorCode = ApplicationErrorCode{ + Code: "unknown_error", + Message: "unknown_error", + } + + UnexpectedCode ApplicationErrorCode = ApplicationErrorCode{ + Code: "unexpected_error", + Message: "unexpected_error", + } +) diff --git a/backend/domain/customerror/errors.go b/backend/domain/customerror/errors.go index a4db177..7e87cb8 100644 --- a/backend/domain/customerror/errors.go +++ b/backend/domain/customerror/errors.go @@ -8,13 +8,17 @@ import ( // ApplicationError はアプリケーション特有のエラー情報を保持する基本構造体です。 type ApplicationError struct { - Code int - Message string + // StatusCode(JSON parseでは構造体省略) + Status int `json:"-"` + // Code はエラーコードを表します。 + Code string `json:"code"` + // Message はエラーメッセージを表します。 + Message string `json:"message"` } // Error メソッドは error インターフェースを実装します。 func (e *ApplicationError) Error() string { - return fmt.Sprintf("Code: %d, Message: %s", e.Code, e.Message) + return fmt.Sprintf("Code: %s, Message: %s", e.Code, e.Message) } // WrongEmailVerificationError は特定の条件下で利用されるカスタムエラーです。 @@ -30,7 +34,8 @@ func NewWrongEmailVerificationError(isTally, isRedirect bool, redirectQuery stri q, _ := url.ParseQuery(redirectQuery) return &WrongEmailVerificationError{ ApplicationError: ApplicationError{ - Code: http.StatusBadRequest, + Status: http.StatusBadRequest, + Code: "WRONG_EMAIL_VERIFICATION_ERROR", Message: "Wrong email verification code", }, IsTally: isTally, diff --git a/backend/internal/rest/middleware/error/error.go b/backend/internal/rest/middleware/error/error.go index 0848899..fc8b75f 100644 --- a/backend/internal/rest/middleware/error/error.go +++ b/backend/internal/rest/middleware/error/error.go @@ -4,7 +4,6 @@ package error import ( "errors" - "net/http" "github.com/labstack/echo/v4" "github.com/naohito-T/tinyurl/backend/domain/customerror" @@ -15,22 +14,21 @@ import ( // この型アサーションを使うことで、安全に型変換を行いつつ、エラーチェックを同時に実施できます。 func CustomErrorHandler(err error, c echo.Context) { c.Logger().Error("カスタムエラーに入ったよ") - if errors.Is(err, customerror.WrongEmailVerificationErrorInstance) { - c.Logger().Error("これがWrongEmailVerificationErrorアプリケーションエラー") - } - he, ok := err.(*echo.HTTPError) - if !ok { - he = echo.NewHTTPError(http.StatusInternalServerError, err.Error()) - } // ロギング - c.Logger().Error(err) + // c.Logger().Error(err) + appErr := buildError(err, c) + c.JSON(appErr.Status, map[string]string{"code": appErr.Code, "message": appErr.Message}) +} - // エラーレスポンスを送信する例 - if err := c.JSON(he.Code, map[string]string{"message": he.Message.(string)}); err != nil { - c.Logger().Error(err) +func buildError(err error, c echo.Context) *customerror.ApplicationError { + appErr := customerror.ApplicationError{ + Status: customerror.UnexpectedCode.Status, + Code: customerror.UnexpectedCode.Code, + Message: customerror.UnexpectedCode.Message, } + if errors.Is(err, customerror.WrongEmailVerificationErrorInstance) { + c.Logger().Error("これがWrongEmailVerificationErrorアプリケーションエラー") + // return &customerror.ApplicationError{} + } + return &appErr } - -// func buildError(err error, code int) *echo.HTTPError { - -// }