Centralizing error handling to a helpers package
This commit is contained in:
		
							
								
								
									
										26
									
								
								internal/helpers/helpers.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								internal/helpers/helpers.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,26 @@
 | 
			
		||||
package helpers
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"go-udemy-web-1/internal/config"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"runtime/debug"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var app *config.AppConfig
 | 
			
		||||
 | 
			
		||||
// NewHelpers sets up app config for helpers
 | 
			
		||||
func NewHelpers(a *config.AppConfig) {
 | 
			
		||||
	app = a
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ClientError(w http.ResponseWriter, status int) {
 | 
			
		||||
	app.InfoLog.Println("Client error with status of", status)
 | 
			
		||||
	http.Error(w, http.StatusText(status), status)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func ServerError(w http.ResponseWriter, err error) {
 | 
			
		||||
	trace := fmt.Sprintf("%s\n%s", err.Error(), debug.Stack())
 | 
			
		||||
	app.ErrorLog.Println(trace)
 | 
			
		||||
	http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user