32 lines
928 B
Go
32 lines
928 B
Go
package db
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
func IsBadRequest(err error) bool {
|
|
return strings.Contains(err.Error(), "bad request:")
|
|
}
|
|
|
|
func BadRequest(err string) error {
|
|
return fmt.Errorf("bad request: %s", err)
|
|
}
|
|
|
|
func BadRequestNotFound(resource, field string, value any) error {
|
|
errStr := fmt.Sprintf("%s with %s=%v not found", resource, field, value)
|
|
return BadRequest(errStr)
|
|
}
|
|
|
|
func BadRequestNotAssociated(parent, child, parentField, childField string, parentID, childID any) error {
|
|
errStr := fmt.Sprintf("%s with %s=%v not associated to %s with %s=%v",
|
|
child, childField, childID, parent, parentField, parentID)
|
|
return BadRequest(errStr)
|
|
}
|
|
|
|
func BadRequestAssociated(parent, child, parentField, childField string, parentID, childID any) error {
|
|
errStr := fmt.Sprintf("%s with %s=%v already associated to %s with %s=%v",
|
|
child, childField, childID, parent, parentField, parentID)
|
|
return BadRequest(errStr)
|
|
}
|