Repository navigation
Closed
Labels
apibugSomething isn't workingenhancementBuild on top of an already existing feature to mke it better.question
Description
Problem
Currently, the codebase uses two distinct and inconsistent response wrappers:
- Success responses use UnravelDocsResponse.java (to be renamed to
XtractionResponse.java), which has the fieldsint statusCode,String status(always"success"),String message, andT data. - Error responses use ErrorResponse.java, which has the fields
int statusCode,String error,String message, andString errorCode(nullable).
This structural split forces API consumers to implement separate parsing logic based on HTTP status codes. Additionally, success responses lack standard metadata such as request paths, timestamps, or trace IDs, which are vital for front-end analytics and error troubleshooting.
Proposed Solution
Create a unified ApiResponse<T> envelope class that handles both success and failure cases with a consistent schema. Deprecate and remove the separate ErrorResponse and XtractionResponse (formerly UnravelDocsResponse) classes. Update the shared ResponseBuilderService.java and GlobalExceptionHandler.java to construct and return the unified ApiResponse<T> wrapper.
User Stories
- As an API consumer, I want the API to return a single consistent response envelope structure so that my client-side wrapper or SDK can parse the response header metadata, success status, and payloads uniformly.
- As a backend developer, I want to use a unified response wrapper so that I don't have to manage different classes for controller outcomes and exception handlers.
Acceptance Criteria
- A new generic class
ApiResponse<T>is created insrc/main/java/com/xtraction/api/shared/response/ApiResponse.java. - The class
ErrorResponseis deleted. - The class
UnravelDocsResponse(orXtractionResponse) is deleted. - Every REST controller endpoint and exception handler method returns
ApiResponse<T>wrapped inResponseEntity<ApiResponse<T>>(or handled viaResponseBodyAdvice). - The JSON response structure contains:
success(boolean):truefor 2xx codes,falseotherwise.statusCode(int): HTTP status code.message(String): Human-readable status or error description.data(T): The payload (null on errors/empty responses).errors(List): Field validation details (null if not a validation error).errorCode(String): central error code (null on success).timestamp(Instant): ISO-8601 UTC timestamp.path(String): Request URI path.traceId(String): Unique transaction ID.
- Swagger API definitions reflect the unified schema.
Proposed Technical Details
package com.xtraction.api.shared.response;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.Instant;
import java.util.List;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ApiResponse<T> {
private boolean success;
private int statusCode;
private String message;
private T data;
private List<ValidationError> errors;
private String errorCode;
private Instant timestamp;
private String path;
private String traceId;
@Data
@AllArgsConstructor
public static class ValidationError {
private String field;
private Object rejectedValue;
private String message;
}
}Tasks
- Define the
ApiResponseclass. - Remove
ErrorResponse.javaandUnravelDocsResponse.java. - Refactor
ResponseBuilderService.javato constructApiResponse<T>. - Search and replace references across all controller files.
- Update
GlobalExceptionHandler.javato returnApiResponse<Void>. - Run test suite to verify no compile-time or serialization issues exist.
Open Questions / Considerations
- Should the
successfield be a boolean or a string status (e.g."SUCCESS","ERROR")? A boolean is standard and easier to parse in modern single-page applications.
Metadata
Metadata
Assignees
Labels
apibugSomething isn't workingenhancementBuild on top of an already existing feature to mke it better.question
Activity