Skip to content

Commit ff582f1

Browse files
Improve validation error messaging
1 parent 1b4cb93 commit ff582f1

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

domain/job_validator.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ func (v *StaticDatasetValidator) ValidateSourceIDWithExternal(ctx context.Contex
5959
}
6060

6161
if data.Type != zebedee.PageTypeDatasetLandingPage {
62-
log.Error(ctx, data.Type, appErrors.ErrSourceIDInvalid)
63-
return "", appErrors.ErrSourceIDInvalid
62+
log.Error(ctx, data.Type, appErrors.ErrSourceDataTypeInvalid)
63+
return "", appErrors.ErrSourceDataTypeInvalid
6464
}
6565

6666
// Extract and validate title
@@ -89,7 +89,7 @@ func checkZebedeeURIExists(ctx context.Context, client clients.ZebedeeClient, ur
8989
if err != nil {
9090
if errors.As(err, &e) {
9191
if e.ActualCode == http.StatusNotFound {
92-
return zebedee.PageData{}, appErrors.ErrSourceIDInvalid
92+
return zebedee.PageData{}, appErrors.ErrSourceDoesNotExist
9393
}
9494
}
9595
log.Error(ctx, "failed to validate source ID with zebedee", err)
@@ -108,7 +108,7 @@ func checkDatasetIDDoesNotExist(ctx context.Context, client datasetSDK.Clienter,
108108
return appErrors.ErrTargetIDValidation
109109
}
110110

111-
return appErrors.ErrTargetIDInvalid
111+
return appErrors.ErrTargetAlreadyExists
112112
}
113113

114114
// ValidateZebedeeURI validates if the given path is a valid URI

domain/job_validator_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,16 @@ const (
3333
datasetValidID = "found"
3434
)
3535

36+
var (
37+
errTest = errors.New("unexpected error")
38+
)
39+
3640
func TestStaticDatasetValidatorWithExternal(t *testing.T) {
3741
zebedeeMock := &clientMocks.ZebedeeClientMock{
3842
GetPageDataFunc: func(ctx context.Context, userAuthToken, collectionID, lang, path string) (zebedee.PageData, error) {
3943
switch path {
4044
case zebedeeErrorPath:
41-
return zebedee.PageData{}, errors.New("unexpected error")
45+
return zebedee.PageData{}, errTest
4246
case zebedeeValidPath:
4347
return zebedee.PageData{
4448
Type: zebedee.PageTypeDatasetLandingPage,
@@ -71,7 +75,7 @@ func TestStaticDatasetValidatorWithExternal(t *testing.T) {
7175
GetDatasetFunc: func(ctx context.Context, headers datasetSDK.Headers, datasetID string) (datasetModels.Dataset, error) {
7276
switch datasetID {
7377
case datasetErrorID:
74-
return datasetModels.Dataset{}, errors.New("unexpected error")
78+
return datasetModels.Dataset{}, errTest
7579
case datasetValidID:
7680
return datasetModels.Dataset{}, nil
7781
case datasetNotFoundID:
@@ -129,6 +133,7 @@ func TestStaticDatasetValidatorWithExternal(t *testing.T) {
129133

130134
Convey("Then an error should be returned", func() {
131135
So(err, ShouldNotBeNil)
136+
So(err, ShouldEqual, appErrors.ErrSourceIDValidation)
132137

133138
Convey("And the title should be empty", func() {
134139
So(title, ShouldEqual, "")
@@ -137,14 +142,15 @@ func TestStaticDatasetValidatorWithExternal(t *testing.T) {
137142
})
138143
})
139144

140-
Convey("Given a zebedee source ID that returns not found error", t, func() {
145+
Convey("Given a zebedee source ID that returns a not found error", t, func() {
141146
validator := domain.StaticDatasetValidator{}
142147

143148
Convey("When the source is validated", func() {
144149
title, err := validator.ValidateSourceIDWithExternal(ctx, zebedeeNotFoundPath, &mockClientlist, testUserAuthToken)
145150

146151
Convey("Then an error should be returned", func() {
147152
So(err, ShouldNotBeNil)
153+
So(err, ShouldEqual, appErrors.ErrSourceDoesNotExist)
148154

149155
Convey("And the title should be empty", func() {
150156
So(title, ShouldEqual, "")
@@ -161,6 +167,7 @@ func TestStaticDatasetValidatorWithExternal(t *testing.T) {
161167

162168
Convey("Then an error should be returned", func() {
163169
So(err, ShouldNotBeNil)
170+
So(err, ShouldEqual, appErrors.ErrSourceDataTypeInvalid)
164171

165172
Convey("And the title should be empty", func() {
166173
So(title, ShouldEqual, "")
@@ -189,6 +196,7 @@ func TestStaticDatasetValidatorWithExternal(t *testing.T) {
189196

190197
Convey("Then an error should be returned", func() {
191198
So(err, ShouldNotBeNil)
199+
So(err, ShouldEqual, appErrors.ErrTargetAlreadyExists)
192200
})
193201
})
194202
})
@@ -201,6 +209,7 @@ func TestStaticDatasetValidatorWithExternal(t *testing.T) {
201209

202210
Convey("Then an error should be returned", func() {
203211
So(err, ShouldNotBeNil)
212+
So(err, ShouldEqual, appErrors.ErrTargetIDValidation)
204213
})
205214
})
206215
})

errors/errors.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ var (
4242
ErrTargetIDNotProvided = errors.New("target ID not provided")
4343
ErrJobTypeNotProvided = errors.New("job type not provided")
4444
ErrSourceTitleNotFound = errors.New("source title not found or empty")
45-
ErrSourceIDInvalid = errors.New("source ID is invalid")
46-
ErrTargetIDInvalid = errors.New("target ID is invalid")
45+
ErrSourceDoesNotExist = errors.New("source ID does not exist")
46+
ErrTargetAlreadyExists = errors.New("target ID already exists")
4747
ErrJobTypeInvalid = errors.New("job type is invalid")
4848
ErrInternalServerError = errors.New("an unexpected error occurred")
4949
ErrSourceIDValidation = errors.New("source ID failed to validate")
@@ -88,8 +88,9 @@ var (
8888
ErrTargetIDValidation: http.StatusInternalServerError,
8989
ErrJobNumberCounterNotFound: http.StatusInternalServerError,
9090
ErrJobAlreadyRunning: http.StatusConflict,
91-
ErrSourceIDInvalid: http.StatusBadRequest,
92-
ErrTargetIDInvalid: http.StatusBadRequest,
91+
ErrSourceDoesNotExist: http.StatusBadRequest,
92+
ErrTargetAlreadyExists: http.StatusBadRequest,
93+
ErrSourceDataTypeInvalid: http.StatusBadRequest,
9394
ErrJobTypeInvalid: http.StatusBadRequest,
9495
ErrSourceIDZebedeeURIInvalid: http.StatusBadRequest,
9596
ErrTargetIDDatasetIDInvalid: http.StatusBadRequest,
@@ -99,8 +100,6 @@ var (
99100
ErrLimitInvalid: http.StatusBadRequest,
100101
ErrLimitExceeded: http.StatusBadRequest,
101102
ErrJobStateTransitionNotAllowed: http.StatusConflict,
102-
ErrSourceIDInvalid: http.StatusBadRequest,
103-
ErrTargetIDInvalid: http.StatusBadRequest,
104103
ErrJobTypeInvalid: http.StatusBadRequest,
105104
ErrJobStateNotAllowed: http.StatusBadRequest,
106105
ErrSortFieldInvalid: http.StatusBadRequest,

features/create_job.feature

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ Feature: Create a Job
144144
"errors": [
145145
{
146146
"code": 400,
147-
"description": "target ID is invalid"
147+
"description": "target ID already exists"
148148
}
149149
]
150150
}
@@ -175,7 +175,7 @@ Feature: Create a Job
175175
"errors": [
176176
{
177177
"code": 400,
178-
"description": "source ID is invalid"
178+
"description": "source data has incorrect type"
179179
}
180180
]
181181
}
@@ -207,7 +207,7 @@ Feature: Create a Job
207207
"errors": [
208208
{
209209
"code": 400,
210-
"description": "target ID is invalid"
210+
"description": "target ID already exists"
211211
}
212212
]
213213
}

0 commit comments

Comments
 (0)