Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Allow Java devices to customize action errors
29-07-2012, 06:40 PM
Post: #1
Allow Java devices to customize action errors
I've added support in the Java device bindings to customizing the error code and error text for action errors. This is required to make the device comply with UPnP specifications saying which codes should be used for certain errors.

The changes are based on how this is done for the C# bindings, with a couple of small differences described below.

1) Replace ActionError.java by the following code:
Code:
package org.openhome.net.device;

/**
* Thrown when an invoked action fails.  Will not be visible to external code.
*/
public class ActionError extends RuntimeException
{
    private int iErrorCode;

    public ActionError()
    {
        super();
    }

    public ActionError(String aMessage)
    {
        super(aMessage);
    }

    public ActionError(String aMessage, int aErrorCode)
    {
        super(aMessage);
        iErrorCode = aErrorCode;
    }

    /**
     * Return the error code.
     *
     * @return  the error code.
     */
    public int getErrorCode()
    {
        return iErrorCode;
    }
}

2) Add a new method reportActionError() at line 380 in the DvInvocation.java, as follows:
Code:
/**
     * Report an error from action processing in provider code.
     *
     * @param aError       the {@link #ActionError} exception object.
     * @param aActionName  the action name.
     */
    public void reportActionError(ActionError aError, String aActionName)
    {
        String msg = aError.getMessage();
        if (msg == null)
        {
            msg = String.format("Action %s failed", aActionName);
        }
        int errCode = aError.getErrorCode();
        if (errCode == 0)
        {
            errCode = 501;
        }
        reportError(errCode, msg);
    }

3) Change line 339 of DvUpnpJava.tt from:
Code:
invocation.reportError(501, "Invalid XML");
to:
Code:
invocation.reportActionError(ae, "<#=a.name#>");

The differences from the corresponding C# code are as follows:

a) The error code is stored as an integer field in the ActionError class instead of being added as a prefix to the text message.

b) The C# code adds 800 to the error code specified by the provider, and the Java code uses the provider-specified code "as is". Using the provider error code "as is" makes it much more straightforward for the provider to send error codes below 800, such as 710 to report an invalid container ID.

I'd also like to add the ability for Java control points to obtain the error code and text from the ProxyError object, but this requires a core runtime change to get the Error object from the Invocation object's private iError field. Is there some reason why ohNet doesn't expose this information to the language bindings?
Find all posts by this user


Messages In This Thread
Allow Java devices to customize action errors - simoncn - 29-07-2012 06:40 PM

Forum Jump: