fix error handling/short-circuiting in multi route matching

This commit is contained in:
Joel Wetzell
2025-12-28 11:30:37 -06:00
parent b639a5c786
commit a994286402
3 changed files with 98 additions and 26 deletions

View File

@@ -20,9 +20,17 @@ type HTTPServer struct {
logger *slog.Logger
}
type ResponseIOError struct {
Index int `json:"index"`
OutputErrors []string `json:"outputErrors"`
ProcessError *string `json:"processError"`
InputError *string `json:"inputError"`
}
type ResponseData struct {
Message string `json:"message"`
Status string `json:"status"`
IOErrors []ResponseIOError `json:"ioErrors"`
Message string `json:"message"`
Status string `json:"status"`
}
func init() {
@@ -43,6 +51,8 @@ func init() {
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
fmt.Printf("%+T", ctx.Value(route.RouterContextKey))
if !ok {
return nil, errors.New("http.server unable to get router from context")
}
@@ -69,14 +79,50 @@ func (hs *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
if hs.router != nil {
routingErrors := hs.router.HandleInput(hs.Id(), r)
if routingErrors != nil {
w.WriteHeader(http.StatusInternalServerError)
response.Status = "error"
response.Message = "routing failed"
aRouteFound, routingErrors := hs.router.HandleInput(hs.Id(), r)
if aRouteFound {
if routingErrors != nil {
w.WriteHeader(http.StatusInternalServerError)
response.Status = "error"
response.Message = "routing failed"
response.IOErrors = []ResponseIOError{}
for _, responseIOError := range routingErrors {
errorToAdd := ResponseIOError{
Index: responseIOError.Index,
}
if responseIOError.InputError != nil {
errorMsg := responseIOError.InputError.Error()
errorToAdd.InputError = &errorMsg
}
if responseIOError.ProcessError != nil {
errorMsg := responseIOError.ProcessError.Error()
errorToAdd.ProcessError = &errorMsg
}
if responseIOError.OutputErrors != nil {
outputErrorMsgs := []string{}
for _, outputError := range responseIOError.OutputErrors {
outputErrorMsgs = append(outputErrorMsgs, outputError.Error())
}
errorToAdd.OutputErrors = outputErrorMsgs
}
response.IOErrors = append(response.IOErrors, errorToAdd)
}
} else {
w.WriteHeader(http.StatusOK)
response.Message = "routing successful"
}
} else {
w.WriteHeader(http.StatusOK)
response.Message = "routing successful"
w.WriteHeader(http.StatusNotFound)
response.Status = "error"
response.Message = "no matching routes found"
}
} else {
w.WriteHeader(http.StatusInternalServerError)