MCPcopy Create free account
hub / github.com/crowdsecurity/crowdsec / Only

Method Only

pkg/database/ent/alert_query.go:207–220  ·  view source on GitHub ↗

Only returns a single Alert entity found by the query, ensuring it only returns one. Returns a *NotSingularError when more than one Alert entity is found. Returns a *NotFoundError when no Alert entities are found.

(ctx context.Context)

Source from the content-addressed store, hash-verified

205// Returns a *NotSingularError when more than one Alert entity is found.
206// Returns a *NotFoundError when no Alert entities are found.
207func (_q *AlertQuery) Only(ctx context.Context) (*Alert, error) {
208 nodes, err := _q.Limit(2).All(setContextOp(ctx, _q.ctx, ent.OpQueryOnly))
209 if err != nil {
210 return nil, err
211 }
212 switch len(nodes) {
213 case 1:
214 return nodes[0], nil
215 case 0:
216 return nil, &NotFoundError{alert.Label}
217 default:
218 return nil, &NotSingularError{alert.Label}
219 }
220}
221
222// OnlyX is like Only, but panics if an error occurs.
223func (_q *AlertQuery) OnlyX(ctx context.Context) *Alert {

Callers 1

OnlyXMethod · 0.95

Calls 3

LimitMethod · 0.95
setContextOpFunction · 0.85
AllMethod · 0.45

Tested by

no test coverage detected