The result types provide a type-safe wrapper for HL7 operation results, distinguishing between successful operations, partial successes, functional rejections, and technical errors.
Hl7Result<T>
Generic result wrapper that encapsulates the outcome of an HL7 operation.
Package: com.hl7client.model.result
Type Parameters
The type of data contained in the result (typically a response DTO)
Fields
The operation status (OK, PARTIAL, REJECTED, or ERROR)
The response data (present for OK, PARTIAL, and REJECTED statuses)
Error information (present for REJECTED and ERROR statuses)
List of item-level errors (present for PARTIAL status)
Factory Methods
ok()
Creates a successful result with data and no errors.
public static < T > Hl7Result < T > ok ( T data)
The successful response data
Returns: Hl7Result<T> with status OK
Example:
ElegibilidadResponse response = // ... parse response
return Hl7Result . ok (response);
partial()
Creates a partially successful result with data and item-level warnings/errors.
public static < T > Hl7Result < T > partial (
T data,
List < Hl7ItemError > details
)
The response data (cannot be null)
details
List<Hl7ItemError>
required
List of item-level errors or warnings
Returns: Hl7Result<T> with status PARTIAL
Example:
List < Hl7ItemError > errors = new ArrayList <>();
for ( RegistracionDetalle item : response . getDetalle ()) {
if ( item . getRecha () != 0 ) {
errors . add ( new Hl7ItemError (
String . valueOf ( item . getRecha ()),
item . getDenoItem (),
Hl7ItemErrorOrigin . DETALLE
));
}
}
if ( ! errors . isEmpty ()) {
return Hl7Result . partial (response, errors);
}
rejected()
Creates a functionally rejected result (business rules violation).
public static < T > Hl7Result < T > rejected (
T data,
Hl7Error functionalError
)
The response data containing rejection information
The functional rejection error
Returns: Hl7Result<T> with status REJECTED
Example:
if ( cabecera . getRechaCabecera () != 0 ) {
Hl7Error error = Hl7Error . functional (
String . valueOf ( cabecera . getRechaCabecera ()),
cabecera . getRechaCabeDeno ()
);
return Hl7Result . rejected (response, error);
}
error()
Creates a technical error result (infrastructure/communication failure).
public static < T > Hl7Result < T > error ( Hl7Error technicalError)
The technical error details
Returns: Hl7Result<T> with status ERROR and no data
Example:
try {
// ... make API call
} catch ( IOException e ) {
return Hl7Result . error (
Hl7Error . technical (
"Network error: " + e . getMessage (),
Hl7ErrorOrigin . TRANSPORTE
)
);
}
Helper Methods
Returns true if status is OK
Returns true if status is PARTIAL
Returns true if status is REJECTED
Returns true if status is ERROR
Accessor Methods
Returns the operation status
Returns the data wrapped in an Optional
Returns the error wrapped in an Optional
Returns an immutable list of item-level errors
Usage Example
Hl7Result < ElegibilidadResponse > result = hl7Service . checkElegibilidad (request);
if ( result . isOk ()) {
ElegibilidadResponse response = result . getData (). get ();
// Process successful response
displayPatientInfo (response);
} else if ( result . isPartial ()) {
ElegibilidadResponse response = result . getData (). get ();
// Process response with warnings
displayPatientInfo (response);
for ( Hl7ItemError detail : result . getDetails ()) {
logger . warn ( "Item warning: {}" , detail . getMessage ());
}
} else if ( result . isRejected ()) {
// Handle business rule rejection
Hl7Error error = result . getIssue (). get ();
showError ( "Request rejected: " + error . getMessage ());
if ( error . isSession ()) {
// Prompt for re-login
redirectToLogin ();
}
} else if ( result . isError ()) {
// Handle technical error
Hl7Error error = result . getIssue (). get ();
logger . error ( "Technical error: {}" , error . getMessage ());
showError ( "System error. Please try again." );
}
Hl7Status
Enumeration of possible operation statuses.
Package: com.hl7client.model.result
Operation completed successfully with no issues
Operation completed with some items having warnings or errors
Operation rejected due to business rules violation
Operation failed due to technical error (network, session, parsing, etc.)
Status Flow Diagram
Hl7Error
Represents an error at the operation level (header/cabecera).
Package: com.hl7client.model.result
Fields
Error code (may be null for technical errors)
Human-readable error message
The origin/layer where the error occurred
Whether this is a session expiration error
Factory Methods
functional()
Creates a functional/business rule error.
public static Hl7Error functional ( String code, String message)
Error code from the HL7 provider
Error message from the HL7 provider
Returns: Hl7Error with origin CABECERA
Example:
Hl7Error error = Hl7Error . functional (
"2001" ,
"Credencial no encontrada"
);
technical()
Creates a technical/infrastructure error.
public static Hl7Error technical ( String message, Hl7ErrorOrigin origin)
The layer where the error occurred
Returns: Hl7Error with no code
Example:
Hl7Error error = Hl7Error . technical (
"Connection timeout" ,
Hl7ErrorOrigin . TRANSPORTE
);
sessionExpired()
Creates a session expiration error.
public static Hl7Error sessionExpired ()
Returns: Hl7Error with predefined session error details
Example:
if ( response . getStatusCode () == 401 ) {
return Hl7Result . error ( Hl7Error . sessionExpired ());
}
Methods
Returns true if this is a session expiration error
Returns the error code (may be null)
Returns the error message
Hl7ErrorOrigin
Enumeration of error origin layers.
Package: com.hl7client.model.result
Error in the response header/cabecera (functional rejection)
Error in response detail items
Network or HTTP transport error
JSON parsing or deserialization error
Error Origin Examples
Show TRANSPORTE - Network Errors
// Connection timeout
Hl7Error . technical (
"Connection timeout after 30s" ,
Hl7ErrorOrigin . TRANSPORTE
)
// HTTP error
Hl7Error . technical (
"HTTP 500: Internal Server Error" ,
Hl7ErrorOrigin . TRANSPORTE
)
// SSL error
Hl7Error . technical (
"SSL handshake failed" ,
Hl7ErrorOrigin . TRANSPORTE
)
Show PARSEO - Parsing Errors
// Invalid JSON
Hl7Error . technical (
"Invalid JSON: unexpected token at line 5" ,
Hl7ErrorOrigin . PARSEO
)
// Missing required field
Hl7Error . technical (
"Missing required field 'cabecera'" ,
Hl7ErrorOrigin . PARSEO
)
// Type mismatch
Hl7Error . technical (
"Expected number but got string for field 'edad'" ,
Hl7ErrorOrigin . PARSEO
)
Show CABECERA - Functional Errors
// Business rule rejection
Hl7Error . functional (
"2001" ,
"Credencial no encontrada"
)
// Coverage issue
Hl7Error . functional (
"3005" ,
"Plan no cubre la prestación solicitada"
)
Hl7ItemError
Represents an error at the item/detail level.
Package: com.hl7client.model.result
Constructor
public Hl7ItemError (
String code,
String message,
Hl7ItemErrorOrigin origin
)
origin
Hl7ItemErrorOrigin
required
The detail level where the error occurred
Methods
Returns the error message
Example
List < Hl7ItemError > errors = new ArrayList <>();
for ( RegistracionDetalle item : response . getDetalle ()) {
if ( item . getRecha () != 0 ) {
errors . add ( new Hl7ItemError (
String . valueOf ( item . getRecha ()),
item . getDenoItem (),
Hl7ItemErrorOrigin . DETALLE
));
}
}
if ( ! errors . isEmpty ()) {
return Hl7Result . partial (response, errors);
}
Hl7ItemErrorOrigin
Enumeration of item-level error origins.
Package: com.hl7client.model.result
Error in a detail item (main item level)
Error in a sub-detail item (nested items like practices, supplies, etc.)
Currently, only DETALLE is actively used in the client implementation.
Complete Example
Here’s a complete example showing how to use all result types together:
public Hl7Result < RegistracionResponse > registrarServicios (
RegistracionRequest request
) {
try {
// Make API call
HttpResponse < String > httpResponse = httpClient . send (
buildRequest (request),
HttpResponse . BodyHandlers . ofString ()
);
// Check session
if ( httpResponse . statusCode () == 401 ) {
return Hl7Result . error ( Hl7Error . sessionExpired ());
}
// Check transport errors
if ( httpResponse . statusCode () >= 500 ) {
return Hl7Result . error (
Hl7Error . technical (
"Server error: " + httpResponse . statusCode (),
Hl7ErrorOrigin . TRANSPORTE
)
);
}
// Parse response
RegistracionResponse response ;
try {
response = objectMapper . readValue (
httpResponse . body (),
RegistracionResponse . class
);
} catch ( JsonProcessingException e ) {
return Hl7Result . error (
Hl7Error . technical (
"Parse error: " + e . getMessage (),
Hl7ErrorOrigin . PARSEO
)
);
}
// Check cabecera rejection
RegistracionCabecera cabecera = response . getCabecera ();
if ( cabecera . getRechaCabecera () != 0 ) {
return Hl7Result . rejected (
response,
Hl7Error . functional (
String . valueOf ( cabecera . getRechaCabecera ()),
cabecera . getRechaCabeDeno ()
)
);
}
// Check for partial success (item-level errors)
if ( response . getDetalle () != null ) {
List < Hl7ItemError > errors = new ArrayList <>();
for ( RegistracionDetalle item : response . getDetalle ()) {
if ( item . getRecha () != 0 ) {
errors . add ( new Hl7ItemError (
String . valueOf ( item . getRecha ()),
item . getDenoItem (),
Hl7ItemErrorOrigin . DETALLE
));
}
}
if ( ! errors . isEmpty ()) {
return Hl7Result . partial (response, errors);
}
}
// Complete success
return Hl7Result . ok (response);
} catch ( IOException e ) {
return Hl7Result . error (
Hl7Error . technical (
"Network error: " + e . getMessage (),
Hl7ErrorOrigin . TRANSPORTE
)
);
} catch ( InterruptedException e ) {
Thread . currentThread (). interrupt ();
return Hl7Result . error (
Hl7Error . technical (
"Request interrupted" ,
Hl7ErrorOrigin . TRANSPORTE
)
);
}
}