Skip to content

Commit

Permalink
fix: The generic errors were missing status codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Matovidlo committed Dec 13, 2024
1 parent ff18572 commit 8958e0c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
40 changes: 24 additions & 16 deletions internal/pkg/service/templates/api/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -609,8 +609,9 @@ func repositoryRef(d dependencies.ProjectRequestScope, name string) (model.Templ
return repo, nil
} else {
return model.TemplateRepository{}, &GenericError{
Name: "templates.repositoryNotFound",
Message: fmt.Sprintf(`Repository "%s" not found.`, name),
StatusCode: http.StatusNotFound,
Name: "templates.repositoryNotFound",
Message: fmt.Sprintf(`Repository "%s" not found.`, name),
}
}
}
Expand Down Expand Up @@ -641,8 +642,9 @@ func templateRecord(ctx context.Context, d dependencies.ProjectRequestScope, rep
tmpl, found := repo.RecordByID(templateID)
if !found {
return nil, nil, &GenericError{
Name: "templates.templateNotFound",
Message: fmt.Sprintf(`Template "%s" not found.`, templateID),
StatusCode: http.StatusNotFound,
Name: "templates.templateNotFound",
Message: fmt.Sprintf(`Template "%s" not found.`, templateID),
}
}
return repo, &tmpl, nil
Expand All @@ -659,15 +661,17 @@ func getTemplateVersion(ctx context.Context, d dependencies.ProjectRequestScope,

if !found {
return nil, nil, &GenericError{
Name: "templates.templateNotFound",
Message: fmt.Sprintf(`Template "%s" not found.`, templateID),
StatusCode: http.StatusNotFound,
Name: "templates.templateNotFound",
Message: fmt.Sprintf(`Template "%s" not found.`, templateID),
}
}

if !hasRequirements(tmplRecord, d) {
return nil, nil, &GenericError{
Name: "templates.templateNoRequirements",
Message: fmt.Sprintf(`Template "%s" doesn't have requirements.`, templateID),
StatusCode: http.StatusBadRequest,
Name: "templates.templateNoRequirements",
Message: fmt.Sprintf(`Template "%s" does not met requirements because of project misconfiguration.`, templateID),
}
}

Expand All @@ -677,8 +681,9 @@ func getTemplateVersion(ctx context.Context, d dependencies.ProjectRequestScope,
// Default version
if versionRecord, err := tmplRecord.DefaultVersionOrErr(); err != nil {
return nil, nil, &GenericError{
Name: "templates.templateNotFound",
Message: err.Error(),
StatusCode: http.StatusNotFound,
Name: "templates.templateNotFound",
Message: err.Error(),
}
} else {
semVersion = versionRecord.Version
Expand All @@ -696,14 +701,16 @@ func getTemplateVersion(ctx context.Context, d dependencies.ProjectRequestScope,
if err != nil {
if errors.As(err, &manifest.TemplateNotFoundError{}) {
return nil, nil, &GenericError{
Name: "templates.templateNotFound",
Message: fmt.Sprintf(`Template "%s" not found.`, templateID),
StatusCode: http.StatusNotFound,
Name: "templates.templateNotFound",
Message: fmt.Sprintf(`Template "%s" not found.`, templateID),
}
}
if errors.As(err, &manifest.VersionNotFoundError{}) {
return nil, nil, &GenericError{
Name: "templates.versionNotFound",
Message: fmt.Sprintf(`Version "%s" not found.`, versionStr),
StatusCode: http.StatusNotFound,
Name: "templates.versionNotFound",
Message: fmt.Sprintf(`Version "%s" not found.`, versionStr),
}
}
return nil, nil, err
Expand Down Expand Up @@ -779,8 +786,9 @@ func getTemplateInstance(ctx context.Context, d dependencies.ProjectRequestScope
instance, found, _ := branch.Local.Metadata.TemplateInstance(instanceId)
if !found {
return nil, branchKey, nil, &GenericError{
Name: "templates.instanceNotFound",
Message: fmt.Sprintf(`Instance "%s" not found in branch "%d".`, instanceId, branchKey.ID),
StatusCode: http.StatusNotFound,
Name: "templates.instanceNotFound",
Message: fmt.Sprintf(`Instance "%s" not found in branch "%d".`, instanceId, branchKey.ID),
}
}
return prjState, branchKey, instance, nil
Expand Down
5 changes: 3 additions & 2 deletions internal/pkg/service/templates/api/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package service

import (
"context"
"net/http"
"testing"
"time"

Expand Down Expand Up @@ -49,7 +50,7 @@ func Test_getTemplateVersion_Requirements(t *testing.T) {
templateID: "template2-required-feature",
projectFeatures: keboola.Features{"my-feature-wrong"},
stackComponents: nil,
exceptedError: &templates.GenericError{Name: "templates.templateNoRequirements", Message: `Template "template2-required-feature" doesn't have requirements.`},
exceptedError: &templates.GenericError{StatusCode: http.StatusBadRequest, Name: "templates.templateNoRequirements", Message: `Template "template2-required-feature" does not met requirements because of project misconfiguration.`},
},
{
name: "required-feature",
Expand All @@ -70,7 +71,7 @@ func Test_getTemplateVersion_Requirements(t *testing.T) {
templateID: "template3-required-component",
projectFeatures: nil,
stackComponents: keboola.Components{{ComponentKey: keboola.ComponentKey{ID: "my.component.wrong"}, Name: "My Component-wrong"}},
exceptedError: &templates.GenericError{Name: "templates.templateNoRequirements", Message: `Template "template3-required-component" doesn't have requirements.`},
exceptedError: &templates.GenericError{StatusCode: http.StatusBadRequest, Name: "templates.templateNoRequirements", Message: `Template "template3-required-component" does not met requirements because of project misconfiguration.`},
},
}

Expand Down

0 comments on commit 8958e0c

Please sign in to comment.