diff --git a/gateway/router/handler.go b/gateway/router/handler.go index ef3b129c..dbc40f3d 100644 --- a/gateway/router/handler.go +++ b/gateway/router/handler.go @@ -188,6 +188,10 @@ func (r *Handler) Handle(ctx context.Context, writer http.ResponseWriter, reques http.Error(writer, err.Error(), http.StatusInternalServerError) return } + if aComponent == nil { + http.Error(writer, "component not available", http.StatusServiceUnavailable) + return + } aResponse, err := r.safelyHandleComponent(ctx, request, aComponent) if err != nil { r.writeErrorResponse(ctx, writer, aComponent, err, http.StatusBadRequest) @@ -237,6 +241,20 @@ func (r *Handler) writeErrorResponse(ctx context.Context, w http.ResponseWriter, execCtx.SetError(err) } responseStatus := r.responseStatusError(message, anObjectErr) + if aComponent == nil || aComponent.Output.Type.Parameters == nil { + errAsBytes, marshalErr := goJson.Marshal(responseStatus) + if marshalErr != nil { + w.WriteHeader(http.StatusInternalServerError) + w.Write([]byte("could not parse error message")) + return + } + if execCtx != nil { + execCtx.StatusCode = statusCode + } + w.WriteHeader(statusCode) + w.Write(errAsBytes) + return + } statusParameter := aComponent.Output.Type.Parameters.LookupByLocation(state.KindOutput, "status") if statusParameter == nil { errAsBytes, marshalErr := goJson.Marshal(responseStatus) diff --git a/gateway/service.go b/gateway/service.go index efd3b7af..794c2b2d 100644 --- a/gateway/service.go +++ b/gateway/service.go @@ -224,6 +224,17 @@ func (r *Service) syncChanges(ctx context.Context, metrics *gmetric.Service, sta return err } r.mux.Lock() + newCount := len(mainRouter.paths) + oldCount := 0 + if r.mainRouter != nil { + oldCount = len(r.mainRouter.paths) + } + if newCount < oldCount { + r.mux.Unlock() + fmt.Printf("[INFO]: routers rebuild skipped (new config has %d routes vs %d existing, keeping existing)\n", newCount, oldCount) + return nil + } + fmt.Printf("[INFO]: router replacing old(%d routes) with new(%d routes)\n", oldCount, newCount) r.mainRouter = mainRouter r.mux.Unlock() fmt.Printf("[INFO]: routers rebuild completed after: %s\n", time.Since(start)) diff --git a/repository/provider.go b/repository/provider.go index ffe16689..b294fcc1 100644 --- a/repository/provider.go +++ b/repository/provider.go @@ -26,7 +26,9 @@ func (p *Provider) Component(ctx context.Context, opts ...Option) (*Component, e p.mux.Lock() defer p.mux.Unlock() if p.control.ChangeKind() == version.ChangeKindDeleted { - //TODO maybe return 404 error + if p.component != nil { + return p.component, nil + } return nil, nil } aComponent, err := p.newComponent(ctx, opts...)