func handleRequest(w http.ResponseWriter, r *http.Request) { if someCondition == false { err := &handler.ResourceErr{ Message: "Resource not found.", Code: http.StatusNotFound, } handler.WriteError(w, err) return } // handle successful request }
func handleError(w http.ResponseWriter, err error) { resourceErr, ok := err.(*handler.ResourceErr) if ok { handler.WriteError(w, resourceErr) } else { // handle non-resource error } }This code shows how to handle errors in a centralized way by defining a function that takes in a writer and an error. If the error is a ResourceErr, it is written to the response using the WriteError function. Otherwise, a non-resource error can be handled in a custom way.