droboticflow-new/internal/api/annotation_validation_dependency.go

45 lines
1.5 KiB
Go

package api
import (
"context"
"errors"
"net/http"
"go.mongodb.org/mongo-driver/mongo"
)
// Keep failed prerequisite reads separate from invalid customer contents.
// Autosave deliberately suppresses repeated 4xx writes of an unchanged draft.
type annotationDictionaryReadError struct{ error }
func (e annotationDictionaryReadError) Unwrap() error { return e.error }
func annotationReadValidationIssue(field, message string, err error) validationIssue {
return validationIssue{Field: field, Code: "annotation_dependency_unavailable", Message: message, dependency: err}
}
func dictionaryReadValidationIssue(field, message string, err error) validationIssue {
var dependency annotationDictionaryReadError
if errors.As(err, &dependency) {
return annotationReadValidationIssue(field, message, err)
}
return validationIssue{Field: field, Message: message}
}
func renderValidationDependencyError(w http.ResponseWriter, issues []validationIssue) bool {
for _, issue := range issues {
if issue.dependency == nil {
continue
}
status := http.StatusServiceUnavailable
if errors.Is(issue.dependency, context.DeadlineExceeded) || mongo.IsTimeout(issue.dependency) {
status = http.StatusGatewayTimeout
}
// Never serialize the database error or mistake an unavailable read for
// success. The existing explicit retry will revalidate permissions/content.
renderJSON(w, status, map[string]any{"error": issue.Message, "code": "annotation_dependency_unavailable", "issues": issues})
return true
}
return false
}