Skip to content

Commit ed15f25

Browse files
authored
Merge branch 'konveyor:main' into main
2 parents 2efcd58 + e4888ee commit ed15f25

File tree

14 files changed

+134
-82
lines changed

14 files changed

+134
-82
lines changed

internal/api/analysis.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -351,22 +351,22 @@ func (h AnalysisHandler) AppCreate(ctx *gin.Context) {
351351
reader := &ManifestReader{}
352352
err = reader.Open(file.Path, BeginMainMarker, EndMainMarker)
353353
if err != nil {
354-
err = &BadRequestError{err.Error()}
354+
err = &BadRequestError{Reason: err.Error()}
355355
_ = ctx.Error(err)
356356
return
357357
} else {
358358
opened = append(opened, reader)
359359
}
360360
d, err := h.Decoder(ctx, file.Encoding, reader)
361361
if err != nil {
362-
err = &BadRequestError{err.Error()}
362+
err = &BadRequestError{Reason: err.Error()}
363363
_ = ctx.Error(err)
364364
return
365365
}
366366
r := &Analysis{}
367367
err = d.Decode(r)
368368
if err != nil {
369-
err = &BadRequestError{err.Error()}
369+
err = &BadRequestError{Reason: err.Error()}
370370
_ = ctx.Error(err)
371371
return
372372
}
@@ -384,15 +384,15 @@ func (h AnalysisHandler) AppCreate(ctx *gin.Context) {
384384
reader = &ManifestReader{}
385385
err = reader.Open(file.Path, BeginInsightsMarker, EndInsightsMarker)
386386
if err != nil {
387-
err = &BadRequestError{err.Error()}
387+
err = &BadRequestError{Reason: err.Error()}
388388
_ = ctx.Error(err)
389389
return
390390
} else {
391391
opened = append(opened, reader)
392392
}
393393
d, err = h.Decoder(ctx, file.Encoding, reader)
394394
if err != nil {
395-
err = &BadRequestError{err.Error()}
395+
err = &BadRequestError{Reason: err.Error()}
396396
_ = ctx.Error(err)
397397
return
398398
}
@@ -403,7 +403,7 @@ func (h AnalysisHandler) AppCreate(ctx *gin.Context) {
403403
if errors.Is(err, io.EOF) {
404404
break
405405
} else {
406-
err = &BadRequestError{err.Error()}
406+
err = &BadRequestError{Reason: err.Error()}
407407
_ = ctx.Error(err)
408408
return
409409
}
@@ -422,15 +422,15 @@ func (h AnalysisHandler) AppCreate(ctx *gin.Context) {
422422
reader = &ManifestReader{}
423423
err = reader.Open(file.Path, BeginDepsMarker, EndDepsMarker)
424424
if err != nil {
425-
err = &BadRequestError{err.Error()}
425+
err = &BadRequestError{Reason: err.Error()}
426426
_ = ctx.Error(err)
427427
return
428428
} else {
429429
opened = append(opened, reader)
430430
}
431431
d, err = h.Decoder(ctx, file.Encoding, reader)
432432
if err != nil {
433-
err = &BadRequestError{err.Error()}
433+
err = &BadRequestError{Reason: err.Error()}
434434
_ = ctx.Error(err)
435435
return
436436
}
@@ -442,7 +442,7 @@ func (h AnalysisHandler) AppCreate(ctx *gin.Context) {
442442
if errors.Is(err, io.EOF) {
443443
break
444444
} else {
445-
err = &BadRequestError{err.Error()}
445+
err = &BadRequestError{Reason: err.Error()}
446446
_ = ctx.Error(err)
447447
return
448448
}
@@ -2144,7 +2144,7 @@ func (r *InsightWriter) Create(id uint, filter qf.Filter) (path string, count in
21442144
case binding.MIMEYAML:
21452145
ext = ".yaml"
21462146
default:
2147-
err = &BadRequestError{"MIME not supported."}
2147+
err = &BadRequestError{Reason: "MIME not supported."}
21482148
}
21492149
file, err := os.CreateTemp("", "insight-*"+ext)
21502150
if err != nil {
@@ -2234,7 +2234,7 @@ func (r *AnalysisWriter) Create(id uint) (path string, err error) {
22342234
case binding.MIMEYAML:
22352235
ext = ".yaml"
22362236
default:
2237-
err = &BadRequestError{"MIME not supported."}
2237+
err = &BadRequestError{Reason: "MIME not supported."}
22382238
}
22392239
file, err := os.CreateTemp("", "report-*"+ext)
22402240
if err != nil {

internal/api/application.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ func (h ApplicationHandler) TagAdd(ctx *gin.Context) {
744744
return
745745
}
746746
if ref.Virtual {
747-
err = &BadRequestError{"cannot add virtual tags"}
747+
err = &BadRequestError{Reason: "cannot add virtual tags"}
748748
_ = ctx.Error(err)
749749
return
750750
}

internal/api/base.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (h *BaseHandler) WithCount(ctx *gin.Context, count int64) (err error) {
6767
if n > MaxPage {
6868
if p.Limit == 0 || p.Limit > MaxPage {
6969
err = &BadRequestError{
70-
fmt.Sprintf(
70+
Reason: fmt.Sprintf(
7171
"Found=%d, ?Limit <= %d required.",
7272
n,
7373
MaxPage)}
@@ -167,10 +167,10 @@ func (h *BaseHandler) Bind(ctx *gin.Context, r any) (err error) {
167167
case binding.MIMEYAML:
168168
err = h.BindYAML(ctx, r)
169169
default:
170-
err = &BadRequestError{"Bind: MIME not supported."}
170+
err = &BadRequestError{Reason: "Bind: MIME not supported."}
171171
}
172172
if err != nil {
173-
err = &BadRequestError{err.Error()}
173+
err = &BadRequestError{Reason: err.Error()}
174174
}
175175
return
176176
}
@@ -242,10 +242,10 @@ func (h *BaseHandler) Decoder(ctx *gin.Context, encoding string, r io.Reader) (d
242242
case binding.MIMEYAML:
243243
d = yaml.NewDecoder(r)
244244
default:
245-
err = &BadRequestError{"Bind: MIME not supported."}
245+
err = &BadRequestError{Reason: "Bind: MIME not supported."}
246246
}
247247
if err != nil {
248-
err = &BadRequestError{err.Error()}
248+
err = &BadRequestError{Reason: err.Error()}
249249
}
250250
return
251251
}

internal/api/batch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (h BatchHandler) create(ctx *gin.Context, create gin.HandlerFunc) {
5757
}
5858

5959
rtx := RichContext(ctx)
60-
bErr := BatchError{Message: "Create failed."}
60+
bErr := &BatchError{Message: "Create failed."}
6161
for i := range resources {
6262
b, _ := json.Marshal(resources[i])
6363
bfr := bytes.NewBuffer(b)

internal/api/bucket.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,12 @@ func (h *BucketOwner) bucketDelete(ctx *gin.Context, id uint) {
273273
func (h *BucketOwner) putDir(ctx *gin.Context, output string) (err error) {
274274
file, err := ctx.FormFile(FileField)
275275
if err != nil {
276-
err = &BadRequestError{err.Error()}
276+
err = &BadRequestError{Reason: err.Error()}
277277
return
278278
}
279279
fileReader, err := file.Open()
280280
if err != nil {
281-
err = &BadRequestError{err.Error()}
281+
err = &BadRequestError{Reason: err.Error()}
282282
_ = ctx.Error(err)
283283
return
284284
}
@@ -325,13 +325,13 @@ func (h *BucketOwner) putFile(ctx *gin.Context, m *model.Bucket) (err error) {
325325
path := pathlib.Join(m.Path, ctx.Param(Wildcard))
326326
input, err := ctx.FormFile(FileField)
327327
if err != nil {
328-
err = &BadRequestError{err.Error()}
328+
err = &BadRequestError{Reason: err.Error()}
329329
_ = ctx.Error(err)
330330
return
331331
}
332332
reader, err := input.Open()
333333
if err != nil {
334-
err = &BadRequestError{err.Error()}
334+
err = &BadRequestError{Reason: err.Error()}
335335
_ = ctx.Error(err)
336336
return
337337
}

internal/api/context.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ func NewEncoder(ctx *gin.Context, output io.Writer) (encoder Encoder, err error)
349349
case binding.MIMEYAML:
350350
encoder = &yamlEncoder{output: output}
351351
default:
352-
err = &BadRequestError{"MIME not supported."}
352+
err = &BadRequestError{Reason: "MIME not supported."}
353353
}
354354

355355
return

internal/api/error.go

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package api
22

33
import (
44
"errors"
5-
"fmt"
65
"net/http"
76
"os"
87

@@ -14,23 +13,19 @@ import (
1413
"github.com/konveyor/tackle2-hub/internal/jsd"
1514
"github.com/konveyor/tackle2-hub/internal/model"
1615
tasking "github.com/konveyor/tackle2-hub/internal/task"
16+
"github.com/konveyor/tackle2-hub/shared/api"
1717
"github.com/mattn/go-sqlite3"
1818
"gorm.io/gorm"
1919
)
2020

2121
// BadRequestError reports bad request errors.
22-
type BadRequestError struct {
23-
Reason string
24-
}
22+
type BadRequestError = api.BadRequestError
2523

26-
func (r *BadRequestError) Error() string {
27-
return r.Reason
28-
}
24+
// Forbidden reports auth errors.
25+
type Forbidden = api.Forbidden
2926

30-
func (r *BadRequestError) Is(err error) (matched bool) {
31-
_, matched = err.(*BadRequestError)
32-
return
33-
}
27+
// NotFound reports resource not-found errors.
28+
type NotFound = api.NotFound
3429

3530
// BatchError reports errors stemming from batch operations.
3631
type BatchError struct {
@@ -43,12 +38,13 @@ type BatchErrorItem struct {
4338
Resource any
4439
}
4540

46-
func (r BatchError) Error() string {
41+
func (r *BatchError) Error() string {
4742
return r.Message
4843
}
4944

50-
func (r BatchError) Is(err error) (matched bool) {
51-
_, matched = err.(BatchError)
45+
func (r *BatchError) Is(err error) (matched bool) {
46+
var target *BatchError
47+
matched = errors.As(err, &target)
5248
return
5349
}
5450

@@ -63,37 +59,8 @@ func (r *TrackerError) Error() string {
6359
}
6460

6561
func (r *TrackerError) Is(err error) (matched bool) {
66-
_, matched = err.(*TrackerError)
67-
return
68-
}
69-
70-
// Forbidden reports auth errors.
71-
type Forbidden struct {
72-
Reason string
73-
}
74-
75-
func (r *Forbidden) Error() string {
76-
return r.Reason
77-
}
78-
79-
func (r *Forbidden) Is(err error) (matched bool) {
80-
_, matched = err.(*Forbidden)
81-
return
82-
}
83-
84-
// NotFound reports resource not-found errors.
85-
type NotFound struct {
86-
Resource string
87-
Reason string
88-
}
89-
90-
func (r *NotFound) Error() string {
91-
return fmt.Sprintf("Resource '%s' not found. %s", r.Resource, r.Reason)
92-
}
93-
94-
func (r *NotFound) Is(err error) (matched bool) {
95-
var forbidden *Forbidden
96-
matched = errors.As(err, &forbidden)
62+
var target *TrackerError
63+
matched = errors.As(err, &target)
9764
return
9865
}
9966

@@ -188,7 +155,7 @@ func ErrorHandler() gin.HandlerFunc {
188155
}
189156

190157
bErr := &BatchError{}
191-
if errors.As(err, bErr) {
158+
if errors.As(err, &bErr) {
192159
rtx.Respond(
193160
http.StatusBadRequest,
194161
bErr.Items,

internal/api/file.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (h FileHandler) Append(ctx *gin.Context) {
8989
var err error
9090
input, err := ctx.FormFile(FileField)
9191
if err != nil {
92-
err = &BadRequestError{err.Error()}
92+
err = &BadRequestError{Reason: err.Error()}
9393
_ = ctx.Error(err)
9494
return
9595
}
@@ -103,7 +103,7 @@ func (h FileHandler) Append(ctx *gin.Context) {
103103
}
104104
reader, err := input.Open()
105105
if err != nil {
106-
err = &BadRequestError{err.Error()}
106+
err = &BadRequestError{Reason: err.Error()}
107107
_ = ctx.Error(err)
108108
return
109109
}
@@ -206,7 +206,7 @@ func (h FileHandler) create(ctx *gin.Context, name string) (m *model.File, err e
206206
func (h FileHandler) createMultipart(ctx *gin.Context, name string) (m *model.File, err error) {
207207
input, err := ctx.FormFile(FileField)
208208
if err != nil {
209-
err = &BadRequestError{err.Error()}
209+
err = &BadRequestError{Reason: err.Error()}
210210
return
211211
}
212212
m = &model.File{}
@@ -227,7 +227,7 @@ func (h FileHandler) createMultipart(ctx *gin.Context, name string) (m *model.Fi
227227
}()
228228
reader, err := input.Open()
229229
if err != nil {
230-
err = &BadRequestError{err.Error()}
230+
err = &BadRequestError{Reason: err.Error()}
231231
return
232232
}
233233
defer func() {

internal/api/jsd/document.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ func (d *Document) Validate(m *jsd.Manager) (err error) {
3939
return
4040
}
4141

42-
// As deserialize the content into the object.
43-
func (d *Document) As(object any) (err error) {
44-
err = d.Content.As(object)
45-
return
46-
}
47-
4842
func (d *Document) With(md *model.Document) {
4943
d.Content = md.Content
5044
d.Schema = md.Schema

internal/api/ruleset.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func (h *RuleSetHandler) update(ctx *gin.Context, r *RuleSet) (err error) {
224224
return
225225
}
226226
if m.Builtin() {
227-
err = &Forbidden{"update on builtin not permitted."}
227+
err = &Forbidden{Reason: "update on builtin not permitted."}
228228
return
229229
}
230230
//
@@ -279,7 +279,7 @@ func (h *RuleSetHandler) delete(ctx *gin.Context, id uint) (err error) {
279279
return
280280
}
281281
if ruleset.Builtin() {
282-
err = &Forbidden{"delete on builtin not permitted."}
282+
err = &Forbidden{Reason: "delete on builtin not permitted."}
283283
return
284284
}
285285
err = h.DB(ctx).Delete(ruleset, id).Error

0 commit comments

Comments
 (0)