PackagePackagetypeA library you install and call yourself.See every one →What this type is →independently maintainedindependently maintainedoriginPublished by one person or a small company outside SAP. Says nothing about quality; the readings say that.See every one →

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

Last pushLicenceMIT

Where it sits

Lives in
Provides
Error handlingABAPError handlingcapabilityTurning a failure into a response a client can act on — protocol-correct error payloads, correlation, and a boundary between internal detail and what a caller sees.

Who is behind it

Furkan COSGUNFurkan COSGUNPersonMaintains this
In the atlas: maintains 6, contributes to 8
@furkancosgun
maintainer

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

Connections1

README

quoted from furkancosgun/EITHER-FOR-ABAP under MIT, read
furkancosgun/EITHER-FOR-ABAP/README.mdOn GitHub

EITHER 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:

  1. Left: Represents error messages or failure results.
  2. 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:

  1. divide_numbers Method: 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.
  2. fetch_data Method: 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.
  3. process_order Method: 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.
  4. complex_operation Method: 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!

MIT, read
Error handling
  1. throwRaises an ABAP exception from a text or the last system messagefrom ABAP
  2. ABAP-PRCFramework for multi-step ABAP processes with monitoringfrom ABAP
  3. com.sap.cds:cds-services-bomMaven bill of materials pinning CAP Java module versionsfrom CAP
  4. cds-error-outboxCaptures CAP service errors, deduplicates them and mails alertsfrom CAP
  5. RESULT-for-ABAPResult pattern for ABAP instead of exceptions for expected failuresfrom ABAP
  6. @sap/cdsCAP Node.js runtimefrom CAP
  7. sap-usi-exceptionShared root exception class and text getter for USI developmentsfrom ABAP

Details

Sources3
dotabapdiscovered herechecked
github.com/furkancosgun/EITHER-FOR-ABAPtree, pushes, contributors, CIchecked
README.mdquoted, and what the entry declareschecked

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
discovered viadotabap
first seen in the atlas
ecosystem and domainabap, protocols-and-apis
abaplint
issues under its own abaplint configuration362
lines760
statements390
objects7
files13
analysed, main
abaplint version2.120.84
History in the atlas1
  1. First seen.
For your agent
Ref
artifact:furkancosgun-either-for-abap
JSON
/artifacts/furkancosgun-either-for-abap.json
MCP
Connecting an agent