Skip to content

Commit

Permalink
refactor(web): Improve error handling and shutdown logic in web frame…
Browse files Browse the repository at this point in the history
…work
  • Loading branch information
youngjun827 committed Nov 7, 2023
1 parent b0a3437 commit 329bbb7
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 1 deletion.
34 changes: 34 additions & 0 deletions business/web/v1/response/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package response

Check failure on line 1 in business/web/v1/response/response.go

View workflow job for this annotation

GitHub Actions / build (1.21.3)

at least one file in a package should have a package comment (ST1000)

import "errors"

type ErrorDocument struct {
Error string `json:"error"`
Fields map[string]string `json:"fields,omitempty"`
}

type Error struct {
Err error
Status int
}

func NewError(err error, status int) error {
return &Error{err, status}
}

func (re *Error) Error() string {
return re.Err.Error()
}

func IsError(err error) bool {
var re *Error
return errors.As(err, &re)
}

func GetError(err error) *Error {
var re *Error
if !errors.As(err, &re) {
return nil
}
return re
}
26 changes: 26 additions & 0 deletions foundation/web/shutdown.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package web

import "errors"

// shutdownError is a type used to help with the graceful termination of the service.
type shutdownError struct {
Message string
}

// NewShutdownError returns an error that causes the framework to signal
// a graceful shutdown.
func NewShutdownError(message string) error {
return &shutdownError{message}
}

// Error is the implementation of the error interface.
func (se *shutdownError) Error() string {
return se.Message
}

// IsShutdown checks to see if the shutdown error is contained
// in the specified error value.
func IsShutdown(err error) bool {
var se *shutdownError
return errors.As(err, &se)
}
25 changes: 24 additions & 1 deletion foundation/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package web

import (
"context"
"errors"
"net/http"
"os"
"syscall"
"time"

"github.com/go-chi/chi/v5"
Expand All @@ -28,6 +30,22 @@ func NewApp(shutdown chan os.Signal, mw ...Middleware) *App {
}
}

func (a *App) SignalShutdown() {
a.shutdown <- syscall.SIGTERM
}

func validateShutdown(err error) bool {
switch {
case errors.Is(err, syscall.EPIPE):
return false

case errors.Is(err, syscall.ECONNRESET):
return false
}

return true
}

func HandlerAdapter(handler Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
err := handler(r.Context(), w, r)
Expand All @@ -50,10 +68,15 @@ func (a *App) Handle(method string, path string, handler Handler, mw ...Middlewa
ctx = SetValues(ctx, &v)
err := wrappedHandler(ctx, w, r)
if err != nil {
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
if validateShutdown(err) {
a.SignalShutdown()
return err
}
}
return nil
}

a.Mux.MethodFunc(method, path, HandlerAdapter(customHandler))
}


0 comments on commit 329bbb7

Please sign in to comment.