I've found the issue causing this.
// This is the `err` I wish to log
if err := validate.Validate.Struct(payload); err != nil {
// err becomes nil here
errResponse, err := utils.GenerateErrorMessages(err)
if err != nil {
utils.WriteJSON(w, http.StatusInternalServerError, err)
return
}
errorMessage := response.ErrorResponse{
Message: "Invalid request body",
Errors: errResponse,
}
// nil err is passed to response writer and not logged.
response.BadRequestErrorResponse(w, r, err, errorMessage)
return
}
I just need to change the use different variable names for the error.