From db957b6cf921548d709474684fa99856ec212360 Mon Sep 17 00:00:00 2001 From: Alberto Scotto Date: Tue, 19 Oct 2021 00:50:28 +0200 Subject: [PATCH] GenericUrl: improve the error message for malformed URLs The exception thrown now clearly shows which was the faulty URL. --- .../java/com/google/api/client/http/GenericUrl.java | 2 +- .../com/google/api/client/http/GenericUrlTest.java | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/google-http-client/src/main/java/com/google/api/client/http/GenericUrl.java b/google-http-client/src/main/java/com/google/api/client/http/GenericUrl.java index 3204f695e..6fe54dfb5 100644 --- a/google-http-client/src/main/java/com/google/api/client/http/GenericUrl.java +++ b/google-http-client/src/main/java/com/google/api/client/http/GenericUrl.java @@ -676,7 +676,7 @@ private static URL parseURL(String encodedUrl) { try { return new URL(encodedUrl); } catch (MalformedURLException e) { - throw new IllegalArgumentException(e); + throw new IllegalArgumentException("Malformed URL: \"" + encodedUrl + "\"", e); } } } diff --git a/google-http-client/src/test/java/com/google/api/client/http/GenericUrlTest.java b/google-http-client/src/test/java/com/google/api/client/http/GenericUrlTest.java index dbe1cc931..8910f3a00 100644 --- a/google-http-client/src/test/java/com/google/api/client/http/GenericUrlTest.java +++ b/google-http-client/src/test/java/com/google/api/client/http/GenericUrlTest.java @@ -14,6 +14,9 @@ package com.google.api.client.http; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + import com.google.api.client.util.Key; import java.net.MalformedURLException; import java.net.URI; @@ -98,6 +101,15 @@ public void testParse_noPath() { assertNull(url.getPathParts()); } + public void testParse_malformedUrl() { + try { + new GenericUrl("http://example.com:8080http://example.com:8080/"); + fail("expected " + IllegalArgumentException.class); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage(), equalTo("Malformed URL: \"http://example.com:8080http://example.com:8080/\"")); + } + } + private static final String SHORT_PATH = "http://bar/path?a=b"; private static final List SHORT_PATH_PARTS = Arrays.asList("", "path");