From 435fff3b1c133e59eff6f944acd1bf7c589a74e8 Mon Sep 17 00:00:00 2001
From: Amanda Anganes <aanganes@mitre.org>
Date: Wed, 27 Mar 2013 16:27:55 -0400
Subject: [PATCH] Updated scope API for error handling

---
 .../java/org/mitre/oauth2/web/ScopeAPI.java   |  6 ++---
 .../src/main/webapp/resources/js/scope.js     | 26 ++++++++++++++-----
 2 files changed, 22 insertions(+), 10 deletions(-)

diff --git a/openid-connect-server/src/main/java/org/mitre/oauth2/web/ScopeAPI.java b/openid-connect-server/src/main/java/org/mitre/oauth2/web/ScopeAPI.java
index 4ac5dee3f..156e587e6 100644
--- a/openid-connect-server/src/main/java/org/mitre/oauth2/web/ScopeAPI.java
+++ b/openid-connect-server/src/main/java/org/mitre/oauth2/web/ScopeAPI.java
@@ -18,7 +18,6 @@ import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestBody;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RequestParam;
 
 import com.google.gson.Gson;
 
@@ -123,7 +122,7 @@ public class ScopeAPI {
 		} catch (RuntimeException e) {
 			logger.error("There was an error attempting to save scope: " + scope + " : " + e.getStackTrace().toString());
 			m.put("code", HttpStatus.BAD_REQUEST);
-			m.put("entity", "An error occurred while processing your request");
+			m.put("entity", "An error occurred while processing your request.");
 			return "jsonEntityView";
 		}
 		
@@ -135,9 +134,10 @@ public class ScopeAPI {
 		} else {
 			
 			logger.error("createScope failed; JSON was invalid: " + json);
+			m.put("entity", "An error occurred while processing your request - invalud JSON.");
 			m.put("code", HttpStatus.BAD_REQUEST);
 			
-			return "httpCodeView";
+			return "jsonEntityView";
 			
 		}
 	}
diff --git a/openid-connect-server/src/main/webapp/resources/js/scope.js b/openid-connect-server/src/main/webapp/resources/js/scope.js
index 6d3743496..76f6cf176 100644
--- a/openid-connect-server/src/main/webapp/resources/js/scope.js
+++ b/openid-connect-server/src/main/webapp/resources/js/scope.js
@@ -84,10 +84,13 @@ var SystemScopeView = Backbone.View.extend({
                         });
                     });
                 },
-            	error:function () {
+            	error:function (error, response) {
+            		
+					//Pull out the response text.
+					var responseText = JSON.parse(response.responseText);
             		
             		//Display an alert with an error message
-            		$('#modalAlert div.modal-body').html("<div class='alert alert-error'><strong>Warning!</strong> An error occurred when processing your request. Please refresh the page and try again.</div>");
+            		$('#modalAlert div.modal-body').html("<div class='alert alert-error'><strong>Warning!</strong>" + responseText + "</div>");
             		
         			 $("#modalAlert").modal({ // wire up the actual modal functionality and show the dialog
         				 "backdrop" : "static",
@@ -236,10 +239,14 @@ var SystemScopeFormView = Backbone.View.extend({
 					app.navigate('admin/scope', {trigger: true});
 				},
 				error:function(error, response) {
-	    			if (response.status == 409) {
+					
+					//Pull out the response text.
+					var responseText = JSON.parse(response.responseText);
+	    			
+					if (response.status == 409) {
 	    				//Conflict, scope already exists
 	    				$('#value.control-group input').addClass('inputError');
-	    				$('#value.control-group').before('<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">&times;</button>A scope with this value already exists, please choose a different value.</div>');
+	    				$('#value.control-group').before('<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">&times;</button>' + responseText + '</div>');
 	    				
 	    				$('#value.control-group').bind('click.error', function() {
 	    					$('#value.control-group input').removeClass('inputError');
@@ -248,9 +255,14 @@ var SystemScopeFormView = Backbone.View.extend({
 	    				
 	    			}
 	    			else {
-	    				//TODO: if there are any other known error cases, catch those by response status and display 
-	    				//appropriate messages.
-	    				$('#value.control-group').before('<div class="alert alert-error"><button type="button" class="close" data-dismiss="alert">&times;</button>A system error occurred when processing your request.</div>');
+	    				//Display an alert with an error message
+	            		$('#modalAlert div.modal-body').html("<div class='alert alert-error'><strong>Warning!</strong>" + responseText + "</div>");
+	            		
+	        			 $("#modalAlert").modal({ // wire up the actual modal functionality and show the dialog
+	        				 "backdrop" : "static",
+	        				 "keyboard" : true,
+	        				 "show" : true // ensure the modal is shown immediately
+	        			 });
 	    			}
 	    		}
 			});