[Opensource] Better error handling process

Kris Thompson kris.thompson at seurat.com
Tue Jul 2 15:15:17 PDT 2002


I made a post a few weeks ago questioning the ErrorTag that comes with 
Expresso 4.02.  My main complaint was that all the error messages had to 
be hard coded into the .java code.  That makes internationalization a 
bit difficult for errors.

So I created a new tag which I have included below which will allow the 
users to add errors in the java code via a key from the 
MessageBundle.properties for that Schema.  Now I can do

errors.addError("error.username.required");


Which in the MessageBundle_en.properties for that file would be

error.username.required=Username required



I would like to propose to have this tag added to Expresso.

Thanks

Kris Thompson





/*  $Id: ErrorMessageBundleTag.java,v 1.1 2002/06/27 21:14:08 kris Exp $
 *
 *  (Comments)
 *
 *  Copyright 2000, XOR, Inc. All Rights Reserved.
 * 
 *  Known Bugs:
 * 
 */
package com.spongbobt.taglib;
import com.jcorporate.expresso.core.controller.*;
import com.jcorporate.expresso.core.i18n.Messages;
import com.jcorporate.expresso.ext.struts.taglib.ControllerUtils;
import com.jcorporate.expresso.ext.taglib.ExpressoTagSupport;
import java.io.IOException;
import java.util.Iterator;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;


import org.apache.struts.action.ActionError;


/**
 *    Description of the Class
 *
 * @author       Created by: kris
 * @created      June 27, 2002
 * @version      $Id: ErrorMessageBundleTag.java,v 1.1 2002/06/27 
21:14:08 kris Exp $
 */
public class ErrorMessageBundleTag extends ExpressoTagSupport {

    /**
     *   
     */
    public ErrorMessageBundleTag() {
        super();
    }

    private ErrorCollection errorCollection = null;
    private String name = null;

    private String nameToUse = null;

    /*
     * getName()
     */


    /**
     *    Insert the method's description here. Creation date: 
(12/29/2000 %r)
     *
     * @param    newName  The new Name value
     * @paramÿ            newName java.lang.String
     */
    public void setName( java.lang.String newName ) {
        name = newName;
    }

    /*
     * doStartTag()
     */

    /**
     *    Setter method for Name.
     *
     * @return      java.lang.String
     */
    public String getName() {
        return nameToUse;
    }

    /*
     * ErrorTag()
     */

    /**
     *    Standard doEndTag.
     *
     * @return                                          int
     * @exception    javax.servlet.jsp.JspTagException  Description of 
Exception
     * @throws                                         
     *      javax.servlet.jsp.JspTagException
     */
    public int doEndTag()
        throws javax.servlet.jsp.JspTagException {

        nameToUse = name;

        try {
            if ( nameToUse == null ) {
                nameToUse = ErrorCollection.ERRORCOLLECTIONKEY;
                //by default
            }
            errorCollection = ( ErrorCollection ) pageContext.getAttribute(
                    nameToUse, PageContext.REQUEST_SCOPE );

            ControllerResponse myResponse =
                    ControllerUtils.getResponse( pageContext );

            String SchemaClass = myResponse.getSchema();

            if ( errorCollection != null ) {
                if ( errorCollection.getErrorCount() > 0 ) {
                    //we have an errorcollection with some errors
                    JspWriter writer = pageContext.getOut();

                    writer.println( myResponse.getString( 
"errors.header" ) );
                    ActionError oneError = null;

                    Iterator iter = iter = errorCollection.get();

                    while ( iter.hasNext() ) {
                        oneError = ( ActionError ) iter.next();
                        writer.println( Messages.getString( SchemaClass, 
oneError.getKey() ) );
                    }

                    writer.println( myResponse.getString( 
"errors.footer" ) );
                }
            }
        } catch ( Exception e ) {
            throw new JspTagException( "ErrorMessageBundleTag: " + 
e.getMessage() );
        }

        return EVAL_PAGE;
    }

    /*
     * doEndTag()
     */

    /**
     *    Do nothing until end tag.
     *
     * @return                                          int
     * @exception    javax.servlet.jsp.JspTagException  Description of 
Exception
     * @throws                                         
     *      javax.servlet.jsp.JspTagException The exception description.
     */
    public int doStartTag()
        throws javax.servlet.jsp.JspTagException {

        return SKIP_BODY;
    }
    /*
     * setName(java.lang.String)
     */


}
/*
 * ErrorTag
 */
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.jcorporate.com/pipermail/opensource/attachments/20020702/409297c0/attachment-0002.htm


More information about the Opensource mailing list