3131import java .io .IOException ;
3232import java .lang .reflect .ParameterizedType ;
3333import java .lang .reflect .Type ;
34- import java .net .ConnectException ;
3534import java .util .ArrayList ;
3635import java .util .Base64 ;
3736import java .util .List ;
@@ -85,7 +84,7 @@ public SendGridClient(SendGridBatchSourceConfig config) {
8584 } else if (config .getAuthType () == SendGridAuthType .BASIC ) {
8685 sendGrid = new SendGridAPIClient (config .getAuthUserName (), config .getAuthPassword ());
8786 } else {
88- throw new IllegalArgumentException (String .format ("Invalid %s authentication method" ,
87+ throw new IllegalArgumentException (String .format ("Invalid authentication method '%s' " ,
8988 SendGridClient .class .getCanonicalName ()));
9089 }
9190 }
@@ -107,10 +106,10 @@ public SendGridClient(String username, String password) {
107106 * @param endpoint relative uri to be queried
108107 * @param arguments arguments for the query
109108 * @return query body
110- * @throws ConnectException if any issue with query the API happen
109+ * @throws IOException if any issue with query the API happen
111110 */
112111 private String makeApiRequest (Method method , String endpoint , @ Nullable Map <String , String > arguments )
113- throws ConnectException {
112+ throws IOException {
114113
115114 Request request = new Request ();
116115 request .setMethod (method );
@@ -124,8 +123,7 @@ private String makeApiRequest(Method method, String endpoint, @Nullable Map<Stri
124123 try {
125124 response = sendGrid .api (request );
126125 } catch (IOException e ) {
127- throw new ConnectException (String .format ("Request to SendGrid API \" %s\" failed with: %s" , endpoint ,
128- e .getMessage ()));
126+ throw new IOException (String .format ("Request to SendGrid API \" %s\" " , endpoint ), e );
129127 }
130128
131129 return response .getBody ();
@@ -136,13 +134,17 @@ private String makeApiRequest(Method method, String endpoint, @Nullable Map<Stri
136134 *
137135 * @param objectInfo objects definition
138136 * @param arguments query arguments
139- * @throws ConnectException if any validation issue
137+ * @throws IllegalArgumentException if any validation issue
140138 */
141- private void checkIncomingArguments (ObjectInfo objectInfo , Map <String , String > arguments ) throws ConnectException {
139+ private void checkIncomingArguments (ObjectInfo objectInfo , Map <String , String > arguments )
140+ throws IllegalArgumentException {
141+
142142 if (objectInfo .getRequiredArguments () != null && !objectInfo .getRequiredArguments ().isEmpty ()) {
143143 if (arguments == null || arguments .isEmpty ()) {
144- throw new ConnectException (String .format ("Object %s require input arguments to be passed, nothing found" ,
145- objectInfo .getCdapObjectName ()));
144+ throw new IllegalArgumentException (String .format (
145+ "Object '%s' require input arguments to be passed, nothing found" ,
146+ objectInfo .getCdapObjectName ()
147+ ));
146148 }
147149 List <String > exceptions = new ArrayList <>();
148150
@@ -155,14 +157,17 @@ private void checkIncomingArguments(ObjectInfo objectInfo, Map<String, String> a
155157 arguments .keySet ().stream ()
156158 .filter (x ::equals )
157159 .findFirst ()
158- .orElseThrow (() -> new ConnectException (String .format ("Object %s require %s argument, but nothing provided" ,
159- objectInfo .getCdapObjectName (), x )));
160- } catch (ConnectException e ) {
160+ .orElseThrow (() -> new IllegalArgumentException (String .format (
161+ "Object '%s' require %s argument, but nothing provided" ,
162+ objectInfo .getCdapObjectName (),
163+ x
164+ )));
165+ } catch (IllegalArgumentException e ) {
161166 exceptions .add (e .getMessage ());
162167 }
163168 });
164169 if (!exceptions .isEmpty ()) {
165- throw new ConnectException (exceptions .stream ().collect (Collectors .joining (System .lineSeparator ())));
170+ throw new IllegalArgumentException (exceptions .stream ().collect (Collectors .joining (System .lineSeparator ())));
166171 }
167172 }
168173 }
@@ -173,9 +178,12 @@ private void checkIncomingArguments(ObjectInfo objectInfo, Map<String, String> a
173178 * @param objectInfo objects definition
174179 * @param arguments query arguments
175180 * @return object representation of the query
176- * @throws ConnectException if any issue with query the API happen
181+ * @throws IOException if any issue with query the API happen
182+ * @throws IllegalStateException unsupported response type caught
183+ * @throws IllegalArgumentException if any validation issue
177184 */
178- public List <IBaseObject > getObject (ObjectInfo objectInfo , Map <String , String > arguments ) throws ConnectException {
185+ public List <IBaseObject > getObject (ObjectInfo objectInfo , Map <String , String > arguments )
186+ throws IOException , IllegalStateException , IllegalArgumentException {
179187 checkIncomingArguments (objectInfo , arguments );
180188
181189 String endpoint = objectInfo .getSendGridAPIUrl ();
@@ -222,8 +230,10 @@ public Type getOwnerType() {
222230
223231 return gson .fromJson (response , typeToken );
224232 } else {
225- throw new ConnectException (String .format ("Unsupported API Response type: %s" ,
226- objectInfo .getApiResponseType ().name ()));
233+ throw new IllegalStateException (String .format (
234+ "Unsupported API Response type: %s" ,
235+ objectInfo .getApiResponseType ().name ()
236+ ));
227237 }
228238 }
229239
@@ -232,13 +242,13 @@ public Type getOwnerType() {
232242 *
233243 * @param objectInfo objects definition
234244 * @return object representation of the query
235- * @throws ConnectException if any issue with query the API happen
245+ * @throws IOException if any issue with query the API happen
246+ * @throws IllegalStateException unsupported response type caught
247+ * @throws IllegalArgumentException if any validation issue
236248 */
237- public List <IBaseObject > getObject (ObjectInfo objectInfo ) throws ConnectException {
249+ public List <IBaseObject > getObject (ObjectInfo objectInfo )
250+ throws IOException , IllegalStateException , IllegalArgumentException {
251+
238252 return getObject (objectInfo , null );
239253 }
240-
241-
242-
243-
244254}
0 commit comments