From e6299c0010212f4f668d130d0081c61e08bfa5e0 Mon Sep 17 00:00:00 2001 From: Vyacheslav1557 Date: Fri, 23 Aug 2024 04:02:14 +0500 Subject: [PATCH] feat: improve error handling --- internal/lib/errors.go | 9 --------- internal/models/result.go | 9 +++++++-- internal/models/role.go | 9 +++++++-- internal/models/testgroup.go | 9 +++++++-- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/internal/lib/errors.go b/internal/lib/errors.go index 92be0c4..888d17d 100644 --- a/internal/lib/errors.go +++ b/internal/lib/errors.go @@ -83,15 +83,6 @@ func location(skip int) string { return fmt.Sprintf("%s:%d", file, line) } -var ( - ErrBadRole = errors.New("bad role") -) - -var ( - ErrBadTestingStrategy = errors.New("bad testing strategy") - ErrBadResult = errors.New("bad result") -) - type Error struct { src error layer layer diff --git a/internal/models/result.go b/internal/models/result.go index c5d5f1a..2cf5b43 100644 --- a/internal/models/result.go +++ b/internal/models/result.go @@ -1,6 +1,9 @@ package models -import "git.sch9.ru/new_gate/ms-tester/internal/lib" +import ( + "errors" + "git.sch9.ru/new_gate/ms-tester/internal/lib" +) type Result int32 @@ -14,10 +17,12 @@ const ( Testing Result = 6 ) +var ErrBadResult = errors.New("bad result") + func (result Result) Valid() error { switch result { case NotTested, Accepted, TimeLimitExceeded, MemoryLimitExceeded, CompilationError, SystemFailDuringTesting: return nil } - return lib.ErrBadResult + return lib.ServiceError(ErrBadResult, lib.ErrValidationFailed, "bad result") } diff --git a/internal/models/role.go b/internal/models/role.go index f3ec8a6..a48cca8 100644 --- a/internal/models/role.go +++ b/internal/models/role.go @@ -1,6 +1,9 @@ package models -import "git.sch9.ru/new_gate/ms-tester/internal/lib" +import ( + "errors" + "git.sch9.ru/new_gate/ms-tester/internal/lib" +) type Role int32 @@ -35,12 +38,14 @@ func (role Role) AtMost(other Role) bool { return role <= other } +var ErrBadRole = errors.New("bad role") + func (role Role) Valid() error { switch role { case RoleSpectator, RoleParticipant, RoleModerator, RoleAdmin: return nil } - return lib.ErrBadRole + return lib.ServiceError(ErrBadRole, lib.ErrValidationFailed, "bad role") } func (role Role) AsPointer() *Role { diff --git a/internal/models/testgroup.go b/internal/models/testgroup.go index 292156d..f102a0c 100644 --- a/internal/models/testgroup.go +++ b/internal/models/testgroup.go @@ -1,6 +1,9 @@ package models -import "git.sch9.ru/new_gate/ms-tester/internal/lib" +import ( + "errors" + "git.sch9.ru/new_gate/ms-tester/internal/lib" +) type TestingStrategy int32 @@ -20,10 +23,12 @@ type TestGroupData struct { TestAmount int32 } +var ErrBadTestingStrategy = errors.New("bad testing strategy") + func (ts TestingStrategy) Valid() error { switch ts { case EachTestTS, CompleteGroupTS: return nil } - return lib.ErrBadTestingStrategy + return lib.ServiceError(ErrBadTestingStrategy, lib.ErrValidationFailed, "bad testing strategy") }