diff src/goodjava/webserver/Connection.java @ 1750:c7b3c327248a

webserver error handling
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 10 Jan 2023 22:42:47 -0700
parents 9713f7fd50b3
children b4fa42c1e999
line wrap: on
line diff
--- a/src/goodjava/webserver/Connection.java	Mon Jan 09 22:06:06 2023 -0700
+++ b/src/goodjava/webserver/Connection.java	Tue Jan 10 22:42:47 2023 -0700
@@ -72,7 +72,11 @@
 					int len = Integer.parseInt(lenStr);
 					byte[] body = new byte[len];
 					size -= endOfHeader;
-					System.arraycopy(a,endOfHeader,body,0,size);
+					try {
+						System.arraycopy(a,endOfHeader,body,0,size);
+					} catch(ArrayIndexOutOfBoundsException e) {
+						throw new WrappedRuntimeException(e);
+					}
 					while( size < len ) {
 						int n = in.read(body,size,len-size);
 						if( n == -1 ) {
@@ -113,7 +117,7 @@
 
 				response = server.handler.handle(request);
 			} catch(ParseException e) {
-				logger.warn("parse error\n"+request.rawHead.trim()+"\n",e);
+				logger.warn("parse error\n"+rawHead.trim()+"\n",e);
 				String msg = e.toString();
 				if( contentType != null )
 					msg = "invalid content for content-type " + contentType + "\n" + msg;
@@ -130,10 +134,19 @@
 			socket.close();
 		} catch(IOException e) {
 			logger.info(rawHead.trim()+"\n",e);
+		} catch(WrappedRuntimeException wrapped) {
+			RuntimeException e = (RuntimeException)wrapped.getCause();
+			logger.info(rawHead.trim()+"\n",e);
+			throw e;
 		} catch(RuntimeException e) {
 			logger.error(rawHead.trim()+"\n",e);
 			throw e;
 		}
 	}
 
+	private static final class WrappedRuntimeException extends Exception {
+		WrappedRuntimeException(RuntimeException e) {
+			super(e);
+		}
+	}
 }