refactor(error): error handling in database package

- Replace custom error types with string constants for common database errors.
- Simplify the Database struct to include a Reason field for error messages.
- Update Error method to format the error output using the Reason.
- Modify validation error constants for consistency and clarity.
This commit is contained in:
SoXX 2024-08-09 22:14:13 +02:00
parent fefc777888
commit b8e29de4fc
2 changed files with 20 additions and 24 deletions

View File

@ -1,25 +1,19 @@
package error package error
type EntityAlreadyExists struct{} import "fmt"
func (e *EntityAlreadyExists) Error() string { const (
return "EntityAlreadyExists error" EntityAlreadyExists = "EntityAlreadyExists"
NoDataWritten = "NoDataWritten"
NoDataFound = "NoDataFound"
DatabaseIsNotConnected = "database is not connected"
DuplicateKey = "DuplicateKey"
)
type Database struct {
Reason string
} }
type NoDataWritten struct{} func (e Database) Error() string {
return fmt.Sprintf("Database error: %s", e.Reason)
func (e *NoDataWritten) Error() string {
return "NoDataWritten error"
}
type NoDataFound struct{}
func (e *NoDataFound) Error() string {
return "NoDataFound error"
}
type NoRelationCreated struct{}
func (e *NoRelationCreated) Error() string {
return "relationship creation error"
} }

View File

@ -3,11 +3,13 @@ package error
import "fmt" import "fmt"
const ( const (
AnthroveUserIDIsEmpty = "anthrovePostID cannot be empty" UserIDIsEmpty = "anthrovePostID cannot be empty"
AnthroveUserIDToShort = "anthrovePostID needs to be 25 characters long" UserIDToShort = "anthrovePostID needs to be 25 characters long"
AnthroveSourceIDEmpty = "anthroveSourceID cannot be empty" SourceIDEmpty = "anthroveSourceID cannot be empty"
AnthroveSourceIDToShort = "anthroveSourceID needs to be 25 characters long" SourceIDToShort = "anthroveSourceID needs to be 25 characters long"
AnthroveTagIDEmpty = "tagID cannot be empty" UserSourceIDEmpty = "anthroveUserSourceID cannot be empty"
UserSourceIDToShort = "anthroveUserSourceID needs to be 25 characters long"
TagIDEmpty = "tagID cannot be empty"
) )
type EntityValidationFailed struct { type EntityValidationFailed struct {