Instead of throwing an exception when a validation fails or something goes wrong, methods return a CrudsResult or a variation of this in order to represent the error state.

When the result is a success, the CrudsResult wraps a return-value, which can be retrieved with the getValue()-method.

CrudsResult<DataRef> success = CrudsResult.success(DataRef.withId(0L));
DataRef dataRef = success.getValue();

When the result is an error, the CrudsResult can be defined with a number of error messages.

CrudsResult<DataRef> error = CrudsResult.error(
      Diagnostic.error("rentWork", "car", "")

Diagnostics represent error messages. They are defined with 3 parameters:

  1. component: the name of the component, rentWork in this case
  2. element: the name of the element, car in this case
  3. key: a key that uniquely defines the error message you wish to propagate, the convention is to define it as component.element.errorDescription or component.element.field.errorDescription

There are also some helper classes that can be used to create cruds error with error messages.

// DiagnosticFactory can create diagnostics
DiagnosticFactory diagnosticFactory = new DiagnosticFactory("rentWork", "car");
Diagnostic errorMsg = diagnosticFactory.error("");

// DiagnosticHelper can create crudsErrors with diagnostics
DiagnosticHelper diagnosticHelper = new DiagnosticHelper("rentWork", "car");
CrudsResult<DataRef> crudsError = diagnosticHelper.createCrudsError("");

// DiagnosticFieldFactory can be used to create diagnostics on field level
DiagnosticFieldFactory fieldFactory = diagnosticFactory.fieldFactory("licensePlate");
Diagnostic fieldErrorMsg = fieldFactory.error("");