When handling failures, choose behavior intentionally: either propagate a real error in a consistent format, or gracefully degrade only for explicitly non-error cases (e.g., notifications, permission edge cases). Avoid masking, asserting, or continuing after validation/parsing fails.
Apply:
return nil, err (or an appropriate response) or explicitly default while logging.return nil, err for failure; avoid returning booleans where callers expect nil, err.assert for expected client/invalid-data scenarios. Return an error response / nil, err instead, so failures become controlled.pcall for environment/phase constraints, handle failures explicitly (e.g., separate “pcall failed due to missing subsystem” from “real operational error”). Don’t let pcall collapse meaningful errors into success paths.Example (Lua error contract + explicit fallback):
local function get_context(headers)
local trace_id = headers["x-instana-t"]
if not trace_id then return nil end
local parsed = trace_id:match("^(%x+)")
if not parsed then
-- invalid input: don't proceed with raw value
return nil, "x-instana-t header invalid"
end
return parsed
end
-- usage
local trace_id, err = get_context(ngx.req.get_headers())
if err then
ngx_log(ngx.WARN, err)
return nil -- or propagate: return nil, err
end
Use logging only when it’s part of the chosen error-handling policy (e.g., non-fatal notification missing handler), and keep error strings consistently formatted (e.g., lowercase-leading).
Enter the URL of a public GitHub repository