fix: json encode and decode use error() instead of nil, err #25
Labels
No labels
coordination/cross-repo
coordination/needed
effort
large
effort
medium
effort
small
meta/duplicate
meta/planning
meta/wontfix
priority
high
priority
low
priority
medium
session
blocker
session
handover
session
next
status
blocked
status
done
status
in-progress
status
review
status
to-go
type
admin
type
bug
type
config
type
deployment
type
docs
type
enhancement
type
feature
type
handover
type
infrastructure
type
installation
type
maintenance
type
migration
type
research
type
security
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
DAW/daw-lua-common#25
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
encode_valueis a recursive local function that callserror()onunsupported types. Returning nil, err through all recursion levels is
not trivial.
Proposed fix
Wrap the top-level call in
builtin_encodewith pcall:local ok, result = pcall(encode_value, value, ...)
if not ok then return nil, "[ERROR] json: " .. result end
return result
See also
fix: encode_value uses error() instead of nil, errto fix: json encode and decode use error() instead of nil, errScope expanded: decode.lua has the same pattern in all three backends (cjson, dkjson, pure-lua). Fix must cover both encode_value and builtin_decode plus the cjson/dkjson wrappers.
builtin_decode's four mutually-recursive functions (decode_string/object/array/value) now thread (value, err) explicitly -- err checked separately from value, since JSON null legitimately decodes to nil. Same pattern applied to encode_value. Also fixed: the cjson branch's pcall(cjson.decode, ...) was catching the error and immediately re-throwing it (error(result)) -- now returns nil, err. cjson encode was previously unprotected (M.encode = cjson.encode directly); now wrapped in pcall for parity with decode.