Managing error messages

HttpServletRequest object's attributes are used to pass error messages from Java programs to other OFBiz resources, including web pages. By convention, error messages are passed as either a single HttpServletRequest attribute name/value pair (where the name is _ERROR_MESSAGE_) or as a list of messages (where the name of the list is _ERROR_MESSAGE_LIST_). All HttpServletRequest object attributes are assumed to be Java Strings.

How to do it...

While you may pass error messages in any fashion you like, the consolidation of error messages and message lists for consumption by other OFBiz resources is made easier if you follow some simple rules:

  1. From within an Event, set the appropriate HttpServletRequest attribute as shown:
    request.setAttribute("_ERROR_MESSAGE_",
    "Error: This is an error message");
    // Or pass a list of error messages
    List myErrors = UtilMisc.toList("This is an error message",
    "This is another error message");
    request.setAttribute("_ERROR_MESSAGE_LIST_", myErrors);
    
  2. From within a Service, you have several options. Best practices suggest never to place the error messages within the HttpServletRequest object directly. Rather, call the appropriate org.ofbiz.Service.ServiceUtil method as shown below:
    // Don't forget to import org.ofbiz.service.ServiceUtil
    // For a successful return from a Service
    // with no error messages to report
    return ServiceUtil.returnSuccess();
    // For error messages that have not be localized
    // or that you want passed as is:
    ServiceUtil.returnError("This is an error");
    // For a list of error messages that you want passed as is
    // back to the context
    List myErrorList = UtilMisc.toList("This is an error message",
    "This is another error message");
    ServiceUtil.returnError(myErrorList);
    // For error messages that have been localized:
    ServiceUtil.returnError(UtilProperties.getMessage(resource_error,
    "SomeErrorMessage",locale));
    // For error messages from an OFBiz exception objects,
    // in this case "e"
    return ServiceUtil.returnError(e.getMessage());
    
  3. To access error messages as a result of a call to an OFBiz Service:
    import org.ofbiz.service.ModelService;
    import org.ofbiz.service.ServiceUtil;
    Map result = dispatcher.runSync("someService", inputParameterMap);
    // Service recalls return response messages.
    // A response message in turn may contain an error message.
    // To get the error message from the results of a Service call:
    if (ModelService.RESPOND_ERROR.equals(
    (String) result.get(ModelService.RESPONSE_MESSAGE))) {
    Map<String, Object> messageMap =
    UtilMisc.toMap("errorMessage",
    result.get(ModelService.ERROR_MESSAGE));
    }
    

There's more...

If you are wondering how errors reported by Java programs and set in the HttpServletRequest object make their way to web pages where they are consumed by browsers and others, then a review of the following FreeMarker template might be in order:

<#if requestAttributes.errorMessageList?has_content>
<#assign errorMessageList=requestAttributes.errorMessageList>
</#if>
<#if requestAttributes.eventMessageList?has_content>
<#assign eventMessageList=requestAttributes.eventMessageList>
</#if>
<#if requestAttributes.serviceValidationException?exists>
<#assign serviceValidationException =
requestAttributes.serviceValidationException>
</#if>
<#if requestAttributes.uiLabelMap?has_content>
<#assign uiLabelMap = requestAttributes.uiLabelMap>
</#if>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.107.100