fleet/server/datastore/mysql/common_mysql/errors.go
Victor Lyuboslavsky 5ee9c0c809
Refactoring common mysql (#26367)
For #26218 

Refactoring android/mysql/testing_utils.go to remove duplication with
main mysql package.
No functional changes.

# Checklist for submitter

- [x] Manual QA for all new/changed functionality
2025-02-18 15:28:54 -06:00

63 lines
1.3 KiB
Go

package common_mysql
import (
"database/sql"
"fmt"
"github.com/fleetdm/fleet/v4/server/fleet"
)
type NotFoundError struct {
ID uint
Name string
Message string
ResourceType string
}
// Compile-time interface check.
var _ fleet.NotFoundError = &NotFoundError{}
func NotFound(kind string) *NotFoundError {
return &NotFoundError{
ResourceType: kind,
}
}
func (e *NotFoundError) Error() string {
if e.ID != 0 {
return fmt.Sprintf("%s %d was not found in the datastore", e.ResourceType, e.ID)
}
if e.Name != "" {
return fmt.Sprintf("%s %s was not found in the datastore", e.ResourceType, e.Name)
}
if e.Message != "" {
return fmt.Sprintf("%s %s was not found in the datastore", e.ResourceType, e.Message)
}
return fmt.Sprintf("%s was not found in the datastore", e.ResourceType)
}
func (e *NotFoundError) WithID(id uint) error {
e.ID = id
return e
}
func (e *NotFoundError) WithName(name string) error {
e.Name = name
return e
}
func (e *NotFoundError) WithMessage(msg string) error {
e.Message = msg
return e
}
func (e *NotFoundError) IsNotFound() bool {
return true
}
// Is helps so that errors.Is(err, sql.ErrNoRows) returns true for an
// error of type *NotFoundError, without having to wrap sql.ErrNoRows
// explicitly.
func (e *NotFoundError) Is(other error) bool {
return other == sql.ErrNoRows
}