001 /* 002 * Copyright © 2008, 2009 Pedro Agulló Soliveres. 003 * 004 * This file is part of DirectJNgine. 005 * 006 * DirectJNgine is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License. 009 * 010 * DirectJNgine is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License 016 * along with DirectJNgine. If not, see <http://www.gnu.org/licenses/>. 017 * 018 * This software uses the ExtJs library (http://extjs.com), which is 019 * distributed under the GPL v3 license (see http://extjs.com/license). 020 */ 021 022 package com.softwarementors.extjs.djn.router; 023 024 import com.google.gson.JsonParseException; 025 import com.softwarementors.extjs.djn.DirectJNgineException; 026 import com.softwarementors.extjs.djn.StringUtils; 027 import com.softwarementors.extjs.djn.config.RegisteredMethod; 028 029 public class JsonException extends DirectJNgineException { 030 031 private JsonException( String message, Throwable cause ) { 032 super(message, cause); 033 034 assert !StringUtils.isEmpty(message); 035 assert cause != null; 036 } 037 038 public static JsonException forFailedConversionFromResponseToJson(ResponseBase response, Throwable ex) { 039 assert response != null; 040 assert ex != null; 041 042 return new JsonException( "Failed attempt to convert a response to json. " + response.getFullLogDescription(), ex ); 043 } 044 045 public static JsonException forFailedConversionFromJsonStringToMethodParameters(RegisteredMethod method, String jsonParametersString, 046 Class<?>[] parameterTypes, JsonParseException ex) 047 { 048 assert method != null; 049 assert jsonParametersString != null; 050 assert parameterTypes != null; 051 assert ex != null; 052 053 StringBuilder typeNames = getCommaSeparatedTypeNames(parameterTypes); 054 return new JsonException( "Failed attempt to convert from a json string to java method parameters. Action='" + method.getActionName() + 055 "', Method='" + method.getName() + "', Json string='" + 056 jsonParametersString + "', ExpectedTypes='" + typeNames + "'", ex ); 057 } 058 059 private static StringBuilder getCommaSeparatedTypeNames(Class<?>[] parameterTypes) 060 { 061 StringBuilder typeNames = new StringBuilder(); 062 for( int i = 0; i < parameterTypes.length -1; i++ ) { 063 Class<?> type = parameterTypes[i]; 064 typeNames.append( type.getName() + ", "); 065 } 066 if( parameterTypes.length > 0 ) { 067 typeNames.append( parameterTypes[parameterTypes.length-1].getName()); 068 } 069 return typeNames; 070 } 071 }