-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added to echo_service_extensions to handle errors on echo and refacto…
…ring common code blocks
- Loading branch information
Showing
7 changed files
with
124 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/labstack/echo/v4" | ||
"github.com/labstack/echo/v4/middleware" | ||
"github.com/rozturac/cerror" | ||
"github.com/rozturac/cerror/extensions" | ||
"net/http" | ||
) | ||
|
||
type EchoErrorResponse struct { | ||
Code string `json:"code"` | ||
Message string `json:"message"` | ||
} | ||
|
||
func main() { | ||
e := echo.New() | ||
e.Use(extensions.AddEchoErrorHandlingMiddlewareWithMapper(func(errorCode, message string, httpStatusCode int) interface{} { | ||
return EchoErrorResponse{ | ||
Code: errorCode, | ||
Message: message, | ||
} | ||
})) | ||
|
||
e.Use(middleware.CORS()) | ||
|
||
e.GET("/example", example) | ||
|
||
e.Start(":8080") | ||
} | ||
|
||
func example(c echo.Context) error { | ||
if name := c.Param("Name"); len(name) == 0 { | ||
panic(cerror.NullReferenceError("name")) | ||
} | ||
|
||
return c.String(http.StatusOK, "Hello, World!") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package extensions | ||
|
||
import ( | ||
"github.com/rozturac/cerror" | ||
"net/http" | ||
) | ||
|
||
type ErrorResponseMapper func(errorCode, message string, httpStatusCode int) interface{} | ||
|
||
func getErrorResponseAndStatusCode(candidateError interface{}, mapper ErrorResponseMapper) (interface{}, int) { | ||
response := mapper("UnExpectedError", http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) | ||
httpStatusCode := http.StatusInternalServerError | ||
|
||
if err, ok := candidateError.(error); ok { | ||
if customError, ok := err.(cerror.Error); ok { | ||
response = mapper(customError.ErrorCode(), customError.Error(), customError.HttpStatusCode()) | ||
httpStatusCode = customError.HttpStatusCode() | ||
} else { | ||
response = mapper("UnExpectedError", err.Error(), http.StatusInternalServerError) | ||
} | ||
} | ||
|
||
return response, httpStatusCode | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package extensions | ||
|
||
import ( | ||
"github.com/labstack/echo/v4" | ||
"net/http" | ||
) | ||
|
||
func AddEchoErrorHandlingMiddlewareWithMapper(mapper ErrorResponseMapper) echo.MiddlewareFunc { | ||
return func(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(context echo.Context) error { | ||
defer func() { | ||
if recover := recover(); recover != nil { | ||
response, httpStatusCode := getErrorResponseAndStatusCode(recover, mapper) | ||
if err := context.JSON(httpStatusCode, response); err != nil { | ||
context.NoContent(http.StatusInternalServerError) | ||
} | ||
} else { | ||
context.NoContent(http.StatusInternalServerError) | ||
} | ||
}() | ||
|
||
if err := next(context); err != nil { | ||
response, httpStatusCode := getErrorResponseAndStatusCode(err, mapper) | ||
if err := context.JSON(httpStatusCode, response); err != nil { | ||
context.NoContent(http.StatusInternalServerError) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package extensions | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
func AddHttpErrorHandlingMiddlewareWithMapper(next http.HandlerFunc, mapper ErrorResponseMapper) http.HandlerFunc { | ||
return func(writer http.ResponseWriter, request *http.Request) { | ||
defer func() { | ||
if recover := recover(); recover != nil { | ||
response, httpStatusCode := getErrorResponseAndStatusCode(recover, mapper) | ||
if buff, err := json.Marshal(response); err != nil { | ||
http.Error(writer, err.Error(), http.StatusInternalServerError) | ||
} else { | ||
http.Error(writer, string(buff), httpStatusCode) | ||
} | ||
} else { | ||
http.Error(writer, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) | ||
} | ||
}() | ||
|
||
next(writer, request) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/rozturac/cerror | ||
|
||
go 1.16 | ||
go 1.16 | ||
|
||
require github.com/labstack/echo/v4 v4.6.1 // indirect |
This file was deleted.
Oops, something went wrong.