-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(errors, validator): improve error handling, added validator
- Loading branch information
1 parent
793b5d6
commit 54414da
Showing
3 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package gapi | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
"github.com/jackc/pgx/v5/pgconn" | ||
"google.golang.org/genproto/googleapis/rpc/errdetails" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func fieldViolation(field string, err error) *errdetails.BadRequest_FieldViolation { | ||
return &errdetails.BadRequest_FieldViolation{ | ||
Field: field, | ||
Description: err.Error(), | ||
} | ||
} | ||
|
||
func invalidArgumentError(violations []*errdetails.BadRequest_FieldViolation) error { | ||
badRequest := &errdetails.BadRequest{FieldViolations: violations} | ||
statusInvalid := status.New(codes.InvalidArgument, "invalid parameters") | ||
|
||
statusDetails, err := statusInvalid.WithDetails(badRequest) | ||
if err != nil { | ||
return statusInvalid.Err() | ||
} | ||
|
||
return statusDetails.Err() | ||
} | ||
|
||
type MultiError []error | ||
|
||
func (me MultiError) Error() string { | ||
var sb strings.Builder | ||
for _, err := range me { | ||
sb.WriteString(err.Error()) | ||
sb.WriteString("\n") | ||
} | ||
return sb.String() | ||
} | ||
|
||
func handleDatabaseError(err error) error { | ||
var pgErr *pgconn.PgError | ||
var errs MultiError | ||
|
||
if errors.As(err, &pgErr) { | ||
switch pgErr.Code { | ||
case "23505": // unique_violation | ||
errs = append(errs, status.Errorf(codes.AlreadyExists, "unique violation occurred: %v", err)) | ||
case "23503": // foreign_key_violation | ||
errs = append(errs, status.Errorf(codes.FailedPrecondition, "foreign key violation occurred: %v", err)) | ||
case "23502": // not_null_violation | ||
errs = append(errs, status.Errorf(codes.InvalidArgument, "not null violation occurred: %v", err)) | ||
case "23514": // check_violation | ||
errs = append(errs, status.Errorf(codes.OutOfRange, "check violation occurred: %v", err)) | ||
case "2200L": // invalid_text_representation | ||
errs = append(errs, status.Errorf(codes.InvalidArgument, "invalid text representation: %v", err)) | ||
case "22P02": // invalid_text_representation | ||
errs = append(errs, status.Errorf(codes.InvalidArgument, "invalid text representation: %v", err)) | ||
case "23P01": // exclusion_violation | ||
errs = append(errs, status.Errorf(codes.AlreadyExists, "exclusion violation occurred: %v", err)) | ||
case "25006": // read_only_sql_transaction | ||
errs = append(errs, status.Errorf(codes.PermissionDenied, "read-only SQL transaction: %v", err)) | ||
case "22023": // no_data | ||
errs = append(errs, status.Errorf(codes.NotFound, "no data: %v", err)) | ||
case "54000": // too_many_connections | ||
errs = append(errs, status.Errorf(codes.ResourceExhausted, "too many connections: %v", err)) | ||
default: | ||
errs = append(errs, status.Errorf(codes.Unknown, "unknown database error: %v", err)) | ||
} | ||
} else { | ||
errs = append(errs, status.Errorf(codes.Internal, "internal error: %v", err)) | ||
} | ||
|
||
if len(errs) > 0 { | ||
return errs | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package validator | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// Function to validate a string value based on the minimum and maximum length | ||
func ValidateString(value string, minLen int, maxLen int) error { | ||
n := len(value) | ||
if n < minLen || n > maxLen { | ||
return fmt.Errorf("must contain %d-%d characters", minLen, maxLen) | ||
} | ||
return nil | ||
} | ||
|
||
// Function to validate UserId | ||
func ValidateUserId(userId int64) error { | ||
// Assuming the UserId should be a positive integer | ||
if userId <= 0 { | ||
return fmt.Errorf("UserId must be a positive integer") | ||
} | ||
return nil | ||
} | ||
|
||
// ValidateDuration checks if the given string can be parsed as a duration | ||
func ValidateDuration(durationStr string) error { | ||
_, err := time.ParseDuration(durationStr) | ||
if err != nil { | ||
return fmt.Errorf("invalid duration format: %w", err) | ||
} | ||
return nil | ||
} |