← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12849: FRED-API: send appropriate error message in json when content-type is not supported

 

------------------------------------------------------------
revno: 12849
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2013-11-01 11:01:06 +0100
message:
  FRED-API: send appropriate error message in json when content-type is not supported
modified:
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityAdvice.java
  dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/MessageUtils.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityAdvice.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityAdvice.java	2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/controller/FacilityAdvice.java	2013-11-01 10:01:06 +0000
@@ -41,11 +41,14 @@
 import org.springframework.http.HttpStatus;
 import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
+import org.springframework.web.HttpMediaTypeNotSupportedException;
 import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 import org.springframework.web.client.HttpClientErrorException;
 import org.springframework.web.client.HttpServerErrorException;
 import org.springframework.web.client.HttpStatusCodeException;
+import org.springframework.web.context.request.WebRequest;
+import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
 
 import java.io.IOException;
 
@@ -53,13 +56,13 @@
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
 @ControllerAdvice
-public class FacilityAdvice
+public class FacilityAdvice extends ResponseEntityExceptionHandler
 {
     //--------------------------------------------------------------------------
     // EXCEPTION HANDLERS
     //--------------------------------------------------------------------------
 
-    @ExceptionHandler( { HttpClientErrorException.class, HttpServerErrorException.class } )
+    @ExceptionHandler({ HttpClientErrorException.class, HttpServerErrorException.class })
     public ResponseEntity<String> statusCodeExceptionHandler( HttpStatusCodeException ex ) throws IOException
     {
         HttpHeaders headers = new HttpHeaders();
@@ -69,7 +72,7 @@
             ex.getMessage() ), headers, ex.getStatusCode() );
     }
 
-    @ExceptionHandler( { DeleteNotAllowedException.class, HierarchyViolationException.class } )
+    @ExceptionHandler({ DeleteNotAllowedException.class, HierarchyViolationException.class })
     public ResponseEntity<String> handleForbidden( Exception ex ) throws IOException
     {
         HttpHeaders headers = new HttpHeaders();
@@ -89,7 +92,7 @@
             ex.getMessage() ), headers, HttpStatus.PRECONDITION_FAILED );
     }
 
-    @ExceptionHandler( { FacilityNotFoundException.class } )
+    @ExceptionHandler({ FacilityNotFoundException.class })
     public ResponseEntity<String> handleNotFound( Exception ex ) throws IOException
     {
         HttpHeaders headers = new HttpHeaders();
@@ -99,7 +102,7 @@
             ex.getMessage() ), headers, HttpStatus.NOT_FOUND );
     }
 
-    @ExceptionHandler( { DuplicateCodeException.class, DuplicateUidException.class, DuplicateUuidException.class } )
+    @ExceptionHandler({ DuplicateCodeException.class, DuplicateUidException.class, DuplicateUuidException.class })
     public ResponseEntity<String> handleConflict( Exception ex ) throws IOException
     {
         HttpHeaders headers = new HttpHeaders();
@@ -108,4 +111,13 @@
         return new ResponseEntity<String>( MessageUtils.jsonMessage( HttpStatus.CONFLICT.toString(),
             ex.getMessage() ), headers, HttpStatus.CONFLICT );
     }
+
+    @Override
+    protected ResponseEntity<Object> handleHttpMediaTypeNotSupported( HttpMediaTypeNotSupportedException ex, HttpHeaders headers, HttpStatus status, WebRequest request )
+    {
+        headers.add( "Content-Type", MediaType.APPLICATION_JSON_VALUE );
+
+        return new ResponseEntity<Object>( MessageUtils.jsonMessage(
+            "415", ex.getMessage() ), HttpStatus.UNSUPPORTED_MEDIA_TYPE );
+    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/MessageUtils.java'
--- dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/MessageUtils.java	2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-web/dhis-web-api-fred/src/main/java/org/hisp/dhis/web/webapi/v1/utils/MessageUtils.java	2013-11-01 10:01:06 +0000
@@ -49,19 +49,28 @@
         objectMapper.setSerializationInclusion( JsonInclude.Include.NON_EMPTY );
     }
 
-    public static String jsonMessage( String message ) throws IOException
+    public static String jsonMessage( String message )
     {
         return messageToJson( new MessageResponse( null, message ) );
     }
 
-    public static String jsonMessage( String code, String message ) throws IOException
+    public static String jsonMessage( String code, String message )
     {
         return messageToJson( new MessageResponse( code, message ) );
     }
 
-    public static String messageToJson( MessageResponse messageResponse ) throws IOException
+    public static String messageToJson( MessageResponse messageResponse )
     {
-        return objectMapper.writeValueAsString( messageResponse );
+        try
+        {
+            return objectMapper.writeValueAsString( messageResponse );
+        }
+        catch ( IOException e )
+        {
+            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
+        }
+
+        throw new IllegalArgumentException();
     }
 
     private MessageUtils()