Class V2EnvelopeWriter
/api/v2 response envelope.
All responses share a common shape:
{ "response": { "status": <int>, ...payload-or-error... } }
status follows the same convention as the v1 JSON API
(0 = success, 1 = user error, 9 = system error)
so existing client SDKs that already branch on this integer continue
to work. The version field is omitted from the envelope because
the API version is already encoded in the URL path (/api/v2/...).
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intStatus value indicating a successful response.static final intStatus value indicating an unexpected server-side failure.static final intStatus value indicating a client-side / user error. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidwriteError(jakarta.servlet.http.HttpServletResponse res, V2ErrorCode code, String message) Writes an error v2 envelope and sets the HTTP status to the code's default.voidwriteErrorWithDetails(jakarta.servlet.http.HttpServletResponse res, V2ErrorCode code, String message, Map<String, Object> details) Writes an error v2 envelope with optional structured details.voidwriteInternalError(jakarta.servlet.http.HttpServletResponse res, Throwable cause, org.apache.logging.log4j.Logger logger, String contextTag) Convenience method for internal error paths.voidwriteSuccess(jakarta.servlet.http.HttpServletResponse res, Map<String, Object> payload) Writes a successful v2 envelope wrapping the supplied payload.
-
Field Details
-
STATUS_OK
public static final int STATUS_OKStatus value indicating a successful response.- See Also:
-
STATUS_USER_ERROR
public static final int STATUS_USER_ERRORStatus value indicating a client-side / user error.- See Also:
-
STATUS_SYSTEM_ERROR
public static final int STATUS_SYSTEM_ERRORStatus value indicating an unexpected server-side failure.- See Also:
-
-
Constructor Details
-
V2EnvelopeWriter
public V2EnvelopeWriter()Creates the v2 envelope writer. Registered as the DI componentv2EnvelopeWriterand obtained viaComponentUtil.getV2EnvelopeWriter().
-
-
Method Details
-
writeSuccess
public void writeSuccess(jakarta.servlet.http.HttpServletResponse res, Map<String, Object> payload) throws IOExceptionWrites a successful v2 envelope wrapping the supplied payload.Does not change the HTTP status (defaults to whatever the container set, normally
200 OK). Sets the response content type toapplication/json; charset=UTF-8.Reserved keys: the payload must not contain the key
"status"; it is owned by the envelope itself. Passing a map that contains that key will throwIllegalStateExceptionso the mistake is caught at development/test time.- Parameters:
res- the HTTP response to write topayload- key/value fields to merge into the envelope;nullis treated as empty- Throws:
IOException- if writing to the response failsIllegalStateException- ifpayloadcontains a reserved envelope key ("status")
-
writeError
public void writeError(jakarta.servlet.http.HttpServletResponse res, V2ErrorCode code, String message) throws IOException Writes an error v2 envelope and sets the HTTP status to the code's default.The envelope's integer
statusis set toSTATUS_SYSTEM_ERRORforV2ErrorCode.INTERNAL_ERRORandSTATUS_USER_ERRORotherwise. Anullmessage is normalised to the empty string so the wire shape stays predictable.Buffer reset contract: when the response is not yet committed, this method calls
ServletResponse.resetBuffer()before writing so that any buffered partial body from a streaming handler (e.g. NDJSON, SSE) is discarded. This guarantees the wire response contains ONLY the error envelope and a cleanapplication/jsonContent-Type — no hybrid responses.- Parameters:
res- the HTTP response to write tocode- the v2 error code; its default HTTP status is applied to the responsemessage- a human-readable message safe to expose to API callers- Throws:
IOException- if writing to the response fails
-
writeErrorWithDetails
public void writeErrorWithDetails(jakarta.servlet.http.HttpServletResponse res, V2ErrorCode code, String message, Map<String, Object> details) throws IOExceptionWrites an error v2 envelope with optional structured details.Behaves exactly like
writeError(HttpServletResponse, V2ErrorCode, String)except that, whendetailsis non-null, the map is embedded aserror.detailsso callers can carry structured diagnostics (e.g. the engine snapshot on aservice_unavailablehealth check) without breaking the canonical envelope shape.Buffer reset contract: when the response is not yet committed, this method calls
ServletResponse.resetBuffer()before writing so any buffered partial body from a streaming handler is discarded.- Parameters:
res- the HTTP response to write tocode- the v2 error code; its default HTTP status is applied to the responsemessage- a human-readable message safe to expose to API callersdetails- optional structured details merged undererror.details;nullomits the key- Throws:
IOException- if writing to the response fails
-
writeInternalError
public void writeInternalError(jakarta.servlet.http.HttpServletResponse res, Throwable cause, org.apache.logging.log4j.Logger logger, String contextTag) throws IOException Convenience method for internal error paths.Safety policy: the cause message is never written to the wire. Instead, it is logged via the caller-supplied logger so the detail is available in server logs without leaking connection strings, stack fragments or other server-internal information to API consumers. If the response is already committed (e.g. SSE or NDJSON handlers that flushed before the failure), the method returns silently to avoid corrupting the in-flight body.
- Parameters:
res- the HTTP response to write tocause- the cause to log (may be null; only logged, never written to wire)logger- the caller's logger to use for WARN-level loggingcontextTag- a brief tag identifying the call site (e.g. "/api/v2/health")- Throws:
IOException- if writing the envelope fails
-