Improve Go query error handling and add non-panicking Get APIs#99
Improve Go query error handling and add non-panicking Get APIs#99Uzer007-admin wants to merge 1 commit into
Conversation
| require.Panics(t, func() { | ||
| _ = DB.Upsert(ns, packItemWrongNamedItem{ID: 4}) | ||
| require.Equal(t, rx.ErrWrongType, DB.Upsert(ns, packItemWrongNamedItem{ID: 4})) | ||
| require.NotPanics(t, func() { |
There was a problem hiding this comment.
NotPanics looks redundant. We've already checked, that DB.Upsert has returned the specific error, so there are no ways it could throw a panic at the same time
(the same comment about other tests nearby)
| } | ||
|
|
||
| // FlatArrayLen is the flat_array_len(field) expression; implements IFunction and IExpression. | ||
| // FlatArrayLen is the flat_array_len(field) expression. |
There was a problem hiding this comment.
What's the idea behind removing the comment?
| // Check IExpression interface and list of implementations nearby for more details. | ||
| func (q *Query) WhereExpressions(left IExpression, condition int, right IExpression) *Query { | ||
| q.ser.PutVarCUInt(queryExpressions) | ||
| left.Serialize(&q.ser) |
There was a problem hiding this comment.
I personally prefer the old way with interface: it looked more clean, than the current inlined implementation. Current version has too much duplicated code. Maybe you could return the Serialize call and supplement it with the setErr call?
| q = q.root | ||
| } | ||
| if q.closed { | ||
| q.panicTrace("Close call on already closed query") |
There was a problem hiding this comment.
Probably this panic was actually usefull: it doesn't depend on the user's actions, it's more like debug check for RX developers. If you've faced this specific panic, it usually means, that something is wrong with the connector itself. Do not think, that we should silently ignore double close
Summary
This PR makes error handling in the Go query builder more predictable. Errors raised while building a query are stored on the root
Queryand returned when the query is executed instead of causing an immediate panic.The following error-returning APIs are added for fetching a single item:
GetErrGetErrCtxGetJsonErrGetJsonErrCtxThe existing
Get,GetCtx,GetJson, andGetJsonCtxmethods preserve their panic-on-error behavior and are implemented as wrappers around the new methods.Changes
Join,Merge, andOncalls;Where,Sort,Set, orSetObject.Exec,ExecToJson,Delete, andUpdatesurface accumulated builder errors through their existing error-returning APIs.ErrWrongTypeinstead of panicking when a value with an incompatible Go type is passed to write or transaction APIs.OpenNamespaceerror instead of panicking on invalidcollateorexpire_afternamespace tags.Set(field, nil)without a reflection panic.REINDEXER_GODEBUG.API and behavior changes
This PR contains intentional API and behavior changes:
GetErr,GetErrCtx,GetJsonErr, andGetJsonErrCtx;IFunctioninterface andSerializeFunctionhelper;FlatArrayLenandNowno longer implementIFunction, while remaining supported query expressions;WhereExpressionsnow accepts only the built-in supported expression types. CustomIExpressionimplementations produce an unsupported-expression error;ErrGet*methods continue to panic on errors.Tests
Added coverage for:
GetErr*APIs;Get*wrappers;collateandexpire_afternamespace tags.