Skip to content

Fix some adapter exceptions not including JSON document location - #3096

Merged
eamonnmcmanus merged 2 commits into
google:mainfrom
Marcono1234:marcono1234/exception-reader-location
Aug 14, 2026
Merged

eamonnmcmanus merged 2 commits into
google:mainfrom
Marcono1234:marcono1234/exception-reader-location

Conversation

@Marcono1234

@Marcono1234 Marcono1234 commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Purpose

Improve exceptions & exception messages

Description

Some of the exceptions thrown by built-in adapters did not include the JSON document location, making it difficult for the user to troubleshoot.

@Marcono1234
Marcono1234 force-pushed the marcono1234/exception-reader-location branch from 0722abe to 666ab6d Compare August 14, 2026 18:28
if (map.containsKey(key)) {
throw new JsonSyntaxException("duplicate key: " + key);
throw new JsonSyntaxException(
"Duplicate key '" + key + "'; at path " + in.getPreviousPath());

@Marcono1234 Marcono1234 Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also changed message to start with capital letter to be consistent with the other exception messages

throw new JsonSyntaxException(
"Duplicate key '" + key + "'; at path " + in.getPreviousPath());
}
V value = valueTypeAdapter.read(in);

@Marcono1234 Marcono1234 Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved reading of the value down so that getPreviousPath() is more accurate (pointing to the key instead of the value)

Comment on lines +732 to +735
String nextString = in.nextString();
if (nextString.equals("null")) {
return null;
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this out of the try-catch (URISyntaxException)

Comment on lines -730 to 741
throw new JsonIOException(e);
throw new JsonSyntaxException(
"Failed parsing '" + nextString + "' as URI; at path " + in.getPreviousPath(), e);
}

@Marcono1234 Marcono1234 Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this from throwing JsonIOException to JsonSyntaxException for consistency; this was the only case where JsonIOException was thrown in this class.

(Technically this is a behavior change, though it is probably unlikely that any user relies on this exact exception.)

@Marcono1234
Marcono1234 marked this pull request as ready for review August 14, 2026 18:28
@Marcono1234
Marcono1234 marked this pull request as draft August 14, 2026 18:32
@Marcono1234
Marcono1234 marked this pull request as ready for review August 14, 2026 18:38
}
try {
return new URL(nextString);
} catch (MalformedURLException e) {

@Marcono1234 Marcono1234 Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added handling for (unchecked1) MalformedURLException, for better exception message and for consistency with URI handling (though there the exception is a checked one)

Footnotes

  1. Edit: Actually a checked exception but a subclass of IOException and therefore needed no try-catch because the enclosing read method already has throws IOException.

@eamonnmcmanus eamonnmcmanus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks great!

I'm guessing that some people will have tests that need to be updated because of these changes, but those tests are fragile anyway. I'm guessing that that includes some of Google's internal tests, which I can easily update.

@eamonnmcmanus
eamonnmcmanus merged commit dae37cf into google:main Aug 14, 2026
21 checks passed
@Marcono1234
Marcono1234 deleted the marcono1234/exception-reader-location branch August 14, 2026 20:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants