Skip to content

Commit

Permalink
implements suggested solution
Browse files Browse the repository at this point in the history
  • Loading branch information
ccoVeille committed Dec 13, 2024
1 parent d1d9774 commit 6399b33
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
6 changes: 6 additions & 0 deletions docs/advanced-guide/gofr-errors/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ dbErr2 := datasource.ErrorDB{Message : "database connection timed out!"}
GoFr's error structs implements an interface with `Error() string` and `StatusCode() int` methods, users can override the
status code by implementing it for their custom error.

You can optionally define a log level for your error with the `LogLevel() logging.Level` methods

#### Usage:
```go
type customError struct {
Expand All @@ -57,4 +59,8 @@ func (c customError) Error() string {
func (c customError) StatusCode() int {
return http.StatusMethodNotAllowed
}

func (c customError) LogLevel() logging.Level {
return logging.WARN
}
```
30 changes: 19 additions & 11 deletions pkg/gofr/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,22 +145,30 @@ func panicRecoveryHandler(re any, log logging.Logger, panicked chan struct{}) {
})
}

//nolint:nestif // Log the error(if any) with traceID and errorMessage.
// Log the error(if any) with traceID and errorMessage.
func (h handler) logError(traceID string, err error) {
if err != nil {
errorLog := &ErrorLogEntry{TraceID: traceID, Error: err.Error()}

if errors.As(err, &gofrHTTP.ErrorEntityAlreadyExist{}) {
h.container.Logger.Info(errorLog)
} else if errors.As(err, &gofrHTTP.ErrorEntityNotFound{}) {
h.container.Logger.Info(errorLog)
} else if errors.As(err, &gofrHTTP.ErrorInvalidParam{}) {
h.container.Logger.Info(errorLog)
} else if errors.As(err, &gofrHTTP.ErrorMissingParam{}) {
h.container.Logger.Info(errorLog)
} else {
h.container.Logger.Error(errorLog)
// define the default log level for error
loggerHelper := h.container.Logger.Error

switch logging.GetLogLevelForError(err) {
case logging.ERROR:
// we use the default log level for error
case logging.INFO:
loggerHelper = h.container.Logger.Info
case logging.NOTICE:
loggerHelper = h.container.Logger.Notice
case logging.DEBUG:
loggerHelper = h.container.Logger.Debug
case logging.WARN:
loggerHelper = h.container.Logger.Warn
case logging.FATAL:
loggerHelper = h.container.Logger.Fatal
}

loggerHelper(errorLog)
}
}

Expand Down
18 changes: 18 additions & 0 deletions pkg/gofr/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,21 @@ func checkIfTerminal(w io.Writer) bool {
func (l *logger) ChangeLevel(level Level) {
l.level = level
}

// LogLevelResponder is an interface that provides a method to get the log level.
type LogLevelResponder interface {
LogLevel() Level
}

// GetLogLevelForError returns the log level for the given error.
// If the error implements [logLevelResponder], its log level is returned.
// Otherwise, the default log level "error" is returned.
func GetLogLevelForError(err error) Level {
level := ERROR

if e, ok := err.(LogLevelResponder); ok {
level = e.LogLevel()
}

return level
}

0 comments on commit 6399b33

Please sign in to comment.