You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@plc4x.apache.org by Julian Feinauer <j....@pragmaticminds.de> on 2018/10/31 20:25:15 UTC

Throw Exception when retrieving non-existing Field in Response

Hi all,

I found some time today to test and toy around a bit with PLC4X and I discovered a behavior which I found weird.
I had the response from a request which “failed” (Response was successful but the Field I wanted was NOT_FOUND).
So my response.getLong(“…”) returned a “null” and a subsequent implicit cast to long finally led to a NPE.

My expected behavior would be an Exception (Runtime Exception).
Also in the case when I try to fetch a filed which is non-existent.
Currently we return null in both cases (see snippet from DefaultPlcReadResponse):

private BaseDefaultFieldItem getFieldInternal(String name) {
    // If this field doesn't exist, ignore it.
    if (values.get(name) == null) {
        return null;
    }
    if (values.get(name).getKey() != PlcResponseCode.OK) {
        return null;
   }
    return values.get(name).getValue();
}

I suggest to throw a Runtime Exception in both cases to give a “valid” Error Message instead of a NPE which is totally unexpected.

Is this okay for everybody? Or are there any concerns?

Julian


Re: Throw Exception when retrieving non-existing Field in Response

Posted by Julian Feinauer <j....@pragmaticminds.de>.
Hi,

thanks for your response.
I changed it.

Julian

Am 01.11.18, 12:30 schrieb "Christofer Dutz" <ch...@c-ware.de>:

    Hi Julian,
    
    I agree that we should throw an exception.
    
    Chris
    
    
    
    Am 31.10.18, 21:25 schrieb "Julian Feinauer" <j....@pragmaticminds.de>:
    
        Hi all,
        
        I found some time today to test and toy around a bit with PLC4X and I discovered a behavior which I found weird.
        I had the response from a request which “failed” (Response was successful but the Field I wanted was NOT_FOUND).
        So my response.getLong(“…”) returned a “null” and a subsequent implicit cast to long finally led to a NPE.
        
        My expected behavior would be an Exception (Runtime Exception).
        Also in the case when I try to fetch a filed which is non-existent.
        Currently we return null in both cases (see snippet from DefaultPlcReadResponse):
        
        private BaseDefaultFieldItem getFieldInternal(String name) {
            // If this field doesn't exist, ignore it.
            if (values.get(name) == null) {
                return null;
            }
            if (values.get(name).getKey() != PlcResponseCode.OK) {
                return null;
           }
            return values.get(name).getValue();
        }
        
        I suggest to throw a Runtime Exception in both cases to give a “valid” Error Message instead of a NPE which is totally unexpected.
        
        Is this okay for everybody? Or are there any concerns?
        
        Julian
        
        
    
    


Re: Throw Exception when retrieving non-existing Field in Response

Posted by Christofer Dutz <ch...@c-ware.de>.
Hi Julian,

I agree that we should throw an exception.

Chris



Am 31.10.18, 21:25 schrieb "Julian Feinauer" <j....@pragmaticminds.de>:

    Hi all,
    
    I found some time today to test and toy around a bit with PLC4X and I discovered a behavior which I found weird.
    I had the response from a request which “failed” (Response was successful but the Field I wanted was NOT_FOUND).
    So my response.getLong(“…”) returned a “null” and a subsequent implicit cast to long finally led to a NPE.
    
    My expected behavior would be an Exception (Runtime Exception).
    Also in the case when I try to fetch a filed which is non-existent.
    Currently we return null in both cases (see snippet from DefaultPlcReadResponse):
    
    private BaseDefaultFieldItem getFieldInternal(String name) {
        // If this field doesn't exist, ignore it.
        if (values.get(name) == null) {
            return null;
        }
        if (values.get(name).getKey() != PlcResponseCode.OK) {
            return null;
       }
        return values.get(name).getValue();
    }
    
    I suggest to throw a Runtime Exception in both cases to give a “valid” Error Message instead of a NPE which is totally unexpected.
    
    Is this okay for everybody? Or are there any concerns?
    
    Julian