diff options
| author | Shireesh Anjal <shireesh@gluster.com> | 2011-07-27 14:12:54 +0530 |
|---|---|---|
| committer | Shireesh Anjal <shireesh@gluster.com> | 2011-07-27 14:42:02 +0530 |
| commit | c0dd21e7d4d1ae1a0f0c120193a350a675534f86 (patch) | |
| tree | 9a414f1900fd0f13c0cb595d6211ebff814b6db0 /src | |
| parent | b8279de05f3c9cd59298bd51a68c5cd6913c5e59 (diff) | |
Exception mapper that converts exceptions into appropriate REST responses.
Diffstat (limited to 'src')
| -rw-r--r-- | src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/v1_0/GenericExceptionMapper.java | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/v1_0/GenericExceptionMapper.java b/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/v1_0/GenericExceptionMapper.java new file mode 100644 index 00000000..f9650902 --- /dev/null +++ b/src/com.gluster.storage.management.server/src/com/gluster/storage/management/server/resources/v1_0/GenericExceptionMapper.java @@ -0,0 +1,54 @@ +/******************************************************************************* + * Copyright (c) 2011 Gluster, Inc. <http://www.gluster.com> + * This file is part of Gluster Management Console. + * + * Gluster Management Console is free software; you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License as published + * by the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * Gluster Management Console is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License + * for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see + * <http://www.gnu.org/licenses/>. + *******************************************************************************/ +package com.gluster.storage.management.server.resources.v1_0; + +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.ResponseBuilder; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +import com.gluster.storage.management.core.exceptions.GlusterValidationException; + +@Provider +public class GenericExceptionMapper implements ExceptionMapper<Exception> { + + /* (non-Javadoc) + * @see javax.ws.rs.ext.ExceptionMapper#toResponse(java.lang.Throwable) + */ + @Override + public Response toResponse(Exception exception) { + ResponseBuilder builder; + if (exception instanceof GlusterValidationException) { + builder = Response.status(Response.Status.BAD_REQUEST); + } else { + builder = Response.status(Response.Status.INTERNAL_SERVER_ERROR); + } + + String errMsg = exception.getMessage(); + if(errMsg == null) { + errMsg = "Following exception occurred : " + exception.getClass().getName(); + StackTraceElement[] stackTrace = exception.getStackTrace(); + if(stackTrace.length > 0) { + errMsg += " at [" + stackTrace[0].getClassName() + "][" + stackTrace[0].getLineNumber() + "]"; + } + } + return builder.entity(errMsg).type(MediaType.TEXT_PLAIN).build(); + } +} |
