EITHER-FOR-ABAP
An ABAP class implementing the Either pattern, holding a success value or an error from a method call in one object, with checks for which side it holds and exceptions for misuse.
zcl_either holds one of two values: a right for a successful result, or a left for an error. A caller builds one with left, right, or cond, which picks between the two on a boolean condition, then asks is_left or is_right which side it holds and reads it back with get_left or get_right. The class raises zcx_either_invalid_type when a value does not fit the side it is given to, and zcx_either_invalid_access when the wrong side is read. It covers building and reading that one object; what happens after a caller finds a left value is left to the caller.
Facts
Where it sits
Who is behind it
In the atlas: maintains 6, contributes to 8@furkancosgunmaintainer
Before choosing this
Signals
ActivityQuietNo published releases; repository quiet for 25 months
It publishes no releases, and its repository has been quiet for at least 24 full months. This may be normal for stable software; check whether its current state still fits your use.
What it declares
Every artifact
- Type
- Package
- Origin
- Independently maintained
As a package
- Where it runs
- does not say
- Distributed through
- does not say
Connections
Turning a failure into a response a client can act on
Download
README
quoted from furkancosgun/EITHER-FOR-ABAP under MIT, readEITHER FOR ABAP
EITHER FOR ABAP is a design pattern used in ABAP programming to standardize the management of results and errors. This pattern helps in handling operations that can either succeed with a result or fail with an error, which can be of various types.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contents
Overview
The Either pattern represents the outcome of an operation with two possible results: a successful result (right) or an error message (left). This pattern enhances code readability and error handling by encapsulating results and errors in a consistent manner.
Pattern Definition
The Either pattern provides a structure that can carry two types of values:
- Left: Represents error messages or failure results.
- Right: Represents successful results.
Below is a simplified definition of the zcl_either class:
CLASS zcl_either DEFINITION.
PUBLIC SECTION.
" Creates an instance based on a condition
CLASS-METHODS cond
IMPORTING
!condition TYPE boolean " Condition to determine result
!left_value TYPE any " Value for the left (error)
!right_value TYPE any " Value for the right (success)
RETURNING
VALUE(ro_either) TYPE REF TO zcl_either
RAISING
zcx_either_invalid_type. " Exception for invalid types
" Creates an instance with a left value (error)
CLASS-METHODS left
IMPORTING
!value TYPE any " Error result
RETURNING
VALUE(ro_either) TYPE REF TO zcl_either
RAISING
zcx_either_invalid_type. " Exception for invalid types
" Creates an instance with a right value (success)
CLASS-METHODS right
IMPORTING
!value TYPE any " Success result
RETURNING
VALUE(ro_either) TYPE REF TO zcl_either
RAISING
zcx_either_invalid_type. " Exception for invalid types
" Checks if the instance is a left value
METHODS is_left
RETURNING
VALUE(r_boolean) TYPE boolean. " Returns TRUE if left value
" Checks if the instance is a right value
METHODS is_right
RETURNING
VALUE(r_boolean) TYPE boolean. " Returns TRUE if right value
" Retrieves the left value
METHODS get_left
EXPORTING
VALUE(value) TYPE any " Retrieves left value
RAISING
zcx_either_invalid_access. " Exception for invalid access
" Retrieves the right value
METHODS get_right
EXPORTING
VALUE(value) TYPE any " Retrieves right value
RAISING
zcx_either_invalid_access. " Exception for invalid access
ENDCLASS.
Method Examples
Here are some examples of how the zcl_either class can be used:
-
divide_numbersMethod: Divides two numbers and returns the result or an error message if division by zero is attempted.METHOD divide_numbers. IF iv_denominator = 0. ro_result = zcl_either=>left( 'Division by zero error' ). ELSE. DATA(lv_result) = iv_numerator / iv_denominator. ro_result = zcl_either=>right( lv_result ). ENDIF. ENDMETHOD. -
fetch_dataMethod: Retrieves data based on an ID and returns the data or an error message if the ID is invalid.METHOD fetch_data. IF iv_id < 1. ro_result = zcl_either=>left( 'Invalid ID' ). ELSE. DATA(lv_data) = 'Fetched Data'. ro_result = zcl_either=>right( lv_data ). ENDIF. ENDMETHOD. -
process_orderMethod: Processes an order based on the order ID and returns a success or error message.METHOD process_order. CASE iv_order_id. WHEN 1. ro_result = zcl_either=>right( 'Order processed successfully' ). WHEN 2. ro_result = zcl_either=>left( 'Order already processed' ). WHEN OTHERS. ro_result = zcl_either=>left( 'Order not found' ). ENDCASE. ENDMETHOD. -
complex_operationMethod: Performs a complex operation and handles errors based on the input.METHOD complex_operation. DATA: lv_intermediate TYPE i. IF iv_input < 0. ro_result = zcl_either=>left( 'Negative input not allowed' ). ELSEIF iv_input = 0. ro_result = zcl_either=>left( 'Zero input not valid' ). ELSE. lv_intermediate = iv_input * 2. ro_result = zcl_either=>right( lv_intermediate ). ENDIF. ENDMETHOD.
Usage Example
Here’s an example of how to use the zcl_either class in practice:
START-OF-SELECTION.
DATA: lo_example TYPE REF TO zcl_either_example,
lo_result TYPE REF TO zcl_either,
lv_result TYPE i,
lv_error TYPE string.
" Create an instance of the class
lo_example = NEW zcl_either_example( ).
" Test Example-1: divide_numbers
lo_result = lo_example->divide_numbers(
iv_numerator = 10
iv_denominator = 0
).
IF lo_result->is_left( ).
" Retrieve and display the error message
lo_result->get_left( IMPORTING value = lv_error ).
WRITE: / 'Error in divide_numbers:', lv_error.
ELSE.
" Retrieve and display the result
lo_result->get_right( IMPORTING value = lv_result ).
WRITE: / 'Result of divide_numbers:', lv_result.
ENDIF.
" Test Example-2: fetch_data
lo_result = lo_example->fetch_data(
iv_id = 0
).
IF lo_result->is_left( ).
" Retrieve and display the error message
lo_result->get_left( IMPORTING value = lv_error ).
WRITE: / 'Error in fetch_data:', lv_error.
ELSE.
" Retrieve and display the data
lo_result->get_right( IMPORTING value = lv_result ).
WRITE: / 'Data from fetch_data:', lv_result.
ENDIF.
" Test Example-3: process_order
lo_result = lo_example->process_order(
iv_order_id = 2
).
IF lo_result->is_left( ).
" Retrieve and display the error message
lo_result->get_left( IMPORTING value = lv_error ).
WRITE: / 'Error in process_order:', lv_error.
ELSE.
" Retrieve and display the success message
lo_result->get_right( IMPORTING value = lv_error ).
WRITE: / 'Success message from process_order:', lv_error.
ENDIF.
" Test Example-4: complex_operation
lo_result = lo_example->complex_operation(
iv_input = 5
).
IF lo_result->is_left( ).
" Retrieve and display the error message
lo_result->get_left( IMPORTING value = lv_error ).
WRITE: / 'Error in complex_operation:', lv_error.
ELSE.
" Retrieve and display the result
lo_result->get_right( IMPORTING value = lv_result ).
WRITE: / 'Result of complex_operation:', lv_result.
ENDIF.
Feel free to adjust these suggestions based on your specific needs or the context of your project!
Others providing
Compare all 8 on the Error handling page →- throwthrowA minimalistic way to raise an ABAP exception, from a text passed directly or, with no argument, from the system fields a preceding MESSAGE statement filled, as a static method or through an interface.independent, abapRaises an ABAP exception from a text or the last system messagefrom ABAP
- ABAP-PRCABAP-PRCA framework for custom multi-step ABAP processes: a process class declares the states, and the framework runs each document through them and monitors the run.independent, abapFramework for multi-step ABAP processes with monitoringfrom ABAP
- com.sap.cds:cds-services-bomcom.sap.cds:cds-services-bomThe Maven bill of materials for CAP Java, pinning every com.sap.cds module in a release, from the API and core runtime to the Spring Boot starters and the CQN engine, to the same version.sap official, capMaven bill of materials pinning CAP Java module versionsfrom CAP
- cds-error-outboxcds-error-outboxA CAP plugin that captures errors raised by CAP services into a database entity, deduplicates them so a repeating failure is one alert, and mails it over SMTP or Microsoft Graph.independent, capCaptures CAP service errors, deduplicates them and mails alertsfrom CAP
- RESULT-for-ABAPRESULT-for-ABAPAn ABAP implementation of the result pattern: a method returns an object built with ok or fail instead of raising an exception for an expected failure, and several results combine into one.independent, abapResult pattern for ABAP instead of exceptions for expected failuresfrom ABAP
- @sap/cds@sap/cdsThe CAP Node.js runtime, which compiles the CDS model, serves the services declared in it, answers generic requests before any handler runs, and loads every cds-plugin.sap official, capCAP Node.js runtimefrom CAP
- sap-usi-exceptionsap-usi-exceptionA shared root exception class and text getter API used across SchwarzIT's USI ABAP developments, converting an exception's text into a common format such as BAPIRET2 or SYMSG.independent, abapShared root exception class and text getter for USI developmentsfrom ABAP
Details
Sources
Sources are graph entities: the same page may back several entries, and an unreachable source flags every row that rests on it.
Compatibility and provenance
abaplint
History in the atlas
- First seen.
For your agent
- Ref
artifact:furkancosgun-either-for-abap- JSON
- /artifacts/furkancosgun-either-for-abap.json
- MCP
- Connecting an agent