javajsongrailsgrails-2.2

Socket read IOException not being caught?


In grails, can the socket read failed or ioexception be caught? The following error is triggered even though i have wrapped the error part in try catch block.

ERROR 2021-03-28 08:34:10,170 [ajp-bio-8109-exec-39783] errors.GrailsExceptionResolver: IOException occurred when processing request: [POST] /race/results/
Socket read failed. Stacktrace follows:
java.io.IOException: Socket read failed
    at java.io.FilterInputStream.read(FilterInputStream.java:133)
    at java.io.PushbackInputStream.read(PushbackInputStream.java:186)
    at java.io.InputStreamReader.read(InputStreamReader.java:184)
    at java.io.Reader.read(Reader.java:140)
    at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1485)
    at org.apache.commons.io.IOUtils.copy(IOUtils.java:1461)
    at org.apache.commons.io.IOUtils.copy(IOUtils.java:1436)
    at org.apache.commons.io.IOUtils.toString(IOUtils.java:585)
    at grails.converters.JSON.parse(JSON.java:312)
    at grails.converters.JSON.parse(JSON.java:347)

At the point where the error is thrown i have wrapped it within try catch block as shown below.

 def results(){


        def results


        try {

            results = request.JSON


        } catch (IOException e1) {

            log.error "ERROR WHILE request.json in /results******************************************************************"

            render contentType: "text/json", text: '{"status":"fail"}'
            return

        }

The error is thrown at this point

results = request.JSON

I appreciate any insights. I am using Grails 2.2.

Thanks!

UPDATE:

Here is the full stacktrace. Thanks!

ERROR 2021-03-28 08:34:10,170 [ajp-bio-8109-exec-39783] errors.GrailsExceptionResolver: IOException occurred when processing request: [POST] /race/results/
Socket read failed. Stacktrace follows:
java.io.IOException: Socket read failed
    at java.io.FilterInputStream.read(FilterInputStream.java:133)
    at java.io.PushbackInputStream.read(PushbackInputStream.java:186)
    at java.io.InputStreamReader.read(InputStreamReader.java:184)
    at java.io.Reader.read(Reader.java:140)
    at org.apache.commons.io.IOUtils.copyLarge(IOUtils.java:1485)
    at org.apache.commons.io.IOUtils.copy(IOUtils.java:1461)
    at org.apache.commons.io.IOUtils.copy(IOUtils.java:1436)
    at org.apache.commons.io.IOUtils.toString(IOUtils.java:585)
    at grails.converters.JSON.parse(JSON.java:312)
    at grails.converters.JSON.parse(JSON.java:347)
    at race.results(VirtualRaceController.groovy:2011)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

Solution

  • You'll probably find it is throwing ConverterException, not IOException. Here is the code:

    class ConvertersExtension {
        static getJSON(HttpServletRequest request) {
            JSON.parse(request)
        }
    }
    public class JSON  {
       public static Object parse(HttpServletRequest request) throws ConverterException {
           // blah blah
           try {
                /// blah blah
           }
           catch (IOException e) {
               throw new ConverterException("Error parsing JSON", e);
           }
       }  
    }
    

    If you're using an IDE, right click on the API in question and select "GO To declaration", or whatever the equivilent is in your IDE until you trace your way up to see what throws what.