2

Have a simple method for connection,

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class Connection {

    public boolean connect(URL url, String requestType) {
        HttpURLConnection connection = null;
        try {
            URLConnection urlConnection = url.openConnection();
            if (urlConnection instanceof HttpURLConnection) {
                connection = (HttpURLConnection) urlConnection;
                connection.setRequestMethod(requestType);
                connection.setRequestProperty("Authorization", getAuth()); //$NON-NLS-1$
            }

            if (connection == null) {
                return false;
            }
            connection.connect();
            return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
        } catch (IOException e) {
            return false;
        }
    }
}

In case of URL path not being a valid one, FileNotFoundException is getting logged as an error in AppyDynamics. How to prevent AppDynamics from catching these exceptions since as part of the code its handled as a boolean return but AppDynamics is flooded with FileNotFoundException. Thanks in advance.

Update As per AppDynamics documentation https://docs.appdynamics.com/display/PRO44/Errors+and+Exceptions An HTTP error response, such as a status code 404 or 500 response get recorded as a transaction snapshot error. As i know at this point in my code above response 404 is legitimate. How can I modify my code to prevent AppDynamics showing it up ? Any Suggestions will be helpful.

User
  • 81
  • 2
  • 9
  • Did you manage to resolve this? I am facing same problem. FileNotFoundException is so generic that I do not want to configure my client to ignore it and want AppD not to shout when handled. – shankulk Mar 27 '19 at 07:07
  • I'm facing the same issue and also wanted to know if you found a solution. Calling a downstream webservice that can give 404 as part of the "normal" flow, resulting in a caught FileNotFoundException – Christopher Suarez Feb 24 '20 at 08:54

1 Answers1

0

Are you sure you're not getting the FileNotFound when trying to do getInputStream on the connection later (or in your real code in case this is a simplified example)? Since HttpUrlConnection implements http protocol and 4xx codes are error codes trying to call getInputStream generates FNF. Instead you should call getErrorStream.

I ran your code (without the auth part) and I don't get any FilenotFoundException testing on url's that return 404.

So in this case HttpUrlConnection correctly implements the http protocol and appdynamics correctly catches the error. I'm facing the same issue with jersey wrapping the FnF in a UniformResourceException but after some analyzing it's actually either jersey that should provide ways of checking the status code before returning output or correctly use httpurlconnection, and in our case - the webservice should not return 404 for requests that yields no found results but rather an empty collection.

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Connection {

    public boolean connect(URL url, String requestType) {
        HttpURLConnection connection = null;
        try {
            URLConnection urlConnection = url.openConnection();
            if (urlConnection instanceof HttpURLConnection) {
                connection = (HttpURLConnection) urlConnection;
                connection.setRequestMethod(requestType);
            }
            if (connection == null) {
                return false;
            }
            connection.connect();
            System.out.println(connection.getResponseCode());
            return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
        } catch (IOException e) {
            System.out.println("E" + e.getMessage());
            return false;
        }
    }

    public static void main(String args[])
    {
        Connection con = new Connection();
        try{
        con.connect(new URL("http://www.google.com/asdfasdfsd"), "GET");   
    } catch(MalformedURLException mfe)
    {

    }
    }
}
  • Note: if using Jersey you cannot simply do builder.get(String.class) since that will render exception on 404 (which is correct behaviour both by jersey and HttpURLConnection), if your webservice uses http status codes as part of it's logic then you should do builder.get(ClientResponse.class) so you can always check the status code. 404 is a client error in the http specification. – Christopher Suarez Mar 04 '20 at 10:15