From 8f016530706bbad2d5dc041a0e3a093cbd637474 Mon Sep 17 00:00:00 2001 From: Mattias-Sehlstedt <60173714+Mattias-Sehlstedt@users.noreply.github.com> Date: Sun, 14 Dec 2025 23:27:55 +0100 Subject: [PATCH] [Java] [Spring Client] Add an exception provider interface to allow for easy exception redefinition --- .../codegen/languages/JavaClientCodegen.java | 2 + .../resources/Java/ExceptionProvider.mustache | 68 ++++++++++++++++ .../libraries/restclient/ApiClient.mustache | 48 ++++++++--- .../libraries/webclient/ApiClient.mustache | 48 ++++++++--- .../java/restclient/.openapi-generator/FILES | 1 + .../org/openapitools/client/ApiClient.java | 46 ++++++++--- .../client/ExceptionProvider.java | 79 +++++++++++++++++++ .../.openapi-generator/FILES | 1 + .../org/openapitools/client/ApiClient.java | 46 ++++++++--- .../client/ExceptionProvider.java | 79 +++++++++++++++++++ .../.openapi-generator/FILES | 1 + .../org/openapitools/client/ApiClient.java | 46 ++++++++--- .../client/ExceptionProvider.java | 79 +++++++++++++++++++ .../.openapi-generator/FILES | 1 + .../org/openapitools/client/ApiClient.java | 46 ++++++++--- .../client/ExceptionProvider.java | 79 +++++++++++++++++++ .../.openapi-generator/FILES | 1 + .../org/openapitools/client/ApiClient.java | 46 ++++++++--- .../client/ExceptionProvider.java | 79 +++++++++++++++++++ .../.openapi-generator/FILES | 1 + .../org/openapitools/client/ApiClient.java | 46 ++++++++--- .../client/ExceptionProvider.java | 79 +++++++++++++++++++ .../.openapi-generator/FILES | 1 + .../org/openapitools/client/ApiClient.java | 46 ++++++++--- .../client/ExceptionProvider.java | 79 +++++++++++++++++++ .../.openapi-generator/FILES | 1 + .../org/openapitools/client/ApiClient.java | 46 ++++++++--- .../client/ExceptionProvider.java | 79 +++++++++++++++++++ .../.openapi-generator/FILES | 1 + .../org/openapitools/client/ApiClient.java | 48 ++++++++--- .../client/ExceptionProvider.java | 79 +++++++++++++++++++ .../.openapi-generator/FILES | 1 + .../org/openapitools/client/ApiClient.java | 46 ++++++++--- .../client/ExceptionProvider.java | 79 +++++++++++++++++++ .../.openapi-generator/FILES | 1 + .../org/openapitools/client/ApiClient.java | 48 ++++++++--- .../client/ExceptionProvider.java | 79 +++++++++++++++++++ .../.openapi-generator/FILES | 1 + .../org/openapitools/client/ApiClient.java | 48 ++++++++--- .../client/ExceptionProvider.java | 79 +++++++++++++++++++ .../.openapi-generator/FILES | 1 + .../org/openapitools/client/ApiClient.java | 48 ++++++++--- .../client/ExceptionProvider.java | 79 +++++++++++++++++++ .../.openapi-generator/FILES | 1 + .../org/openapitools/client/ApiClient.java | 48 ++++++++--- .../client/ExceptionProvider.java | 79 +++++++++++++++++++ .../java/restclient/.openapi-generator/FILES | 1 + .../org/openapitools/client/ApiClient.java | 48 ++++++++--- .../client/ExceptionProvider.java | 79 +++++++++++++++++++ .../.openapi-generator/FILES | 1 + .../org/openapitools/client/ApiClient.java | 48 ++++++++--- .../client/ExceptionProvider.java | 79 +++++++++++++++++++ .../.openapi-generator/FILES | 1 + .../org/openapitools/client/ApiClient.java | 46 ++++++++--- .../client/ExceptionProvider.java | 79 +++++++++++++++++++ .../.openapi-generator/FILES | 1 + .../org/openapitools/client/ApiClient.java | 46 ++++++++--- .../client/ExceptionProvider.java | 79 +++++++++++++++++++ .../.openapi-generator/FILES | 1 + .../org/openapitools/client/ApiClient.java | 48 ++++++++--- .../client/ExceptionProvider.java | 79 +++++++++++++++++++ .../.openapi-generator/FILES | 1 + .../org/openapitools/client/ApiClient.java | 48 ++++++++--- .../client/ExceptionProvider.java | 79 +++++++++++++++++++ .../.openapi-generator/FILES | 1 + .../org/openapitools/client/ApiClient.java | 48 ++++++++--- .../client/ExceptionProvider.java | 79 +++++++++++++++++++ .../java/webclient/.openapi-generator/FILES | 1 + .../org/openapitools/client/ApiClient.java | 48 ++++++++--- .../client/ExceptionProvider.java | 79 +++++++++++++++++++ 70 files changed, 2707 insertions(+), 253 deletions(-) create mode 100644 modules/openapi-generator/src/main/resources/Java/ExceptionProvider.mustache create mode 100644 samples/client/echo_api/java/restclient/src/main/java/org/openapitools/client/ExceptionProvider.java create mode 100644 samples/client/others/java/restclient-enum-in-multipart/src/main/java/org/openapitools/client/ExceptionProvider.java create mode 100644 samples/client/others/java/restclient-sealedInterface/src/main/java/org/openapitools/client/ExceptionProvider.java create mode 100644 samples/client/others/java/restclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ExceptionProvider.java create mode 100644 samples/client/others/java/webclient-sealedInterface/src/main/java/org/openapitools/client/ExceptionProvider.java create mode 100644 samples/client/others/java/webclient-sealedInterface_3_1/src/main/java/org/openapitools/client/ExceptionProvider.java create mode 100644 samples/client/others/java/webclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ExceptionProvider.java create mode 100644 samples/client/petstore/java/restclient-nullable-arrays/src/main/java/org/openapitools/client/ExceptionProvider.java create mode 100644 samples/client/petstore/java/restclient-springBoot4-jackson2/src/main/java/org/openapitools/client/ExceptionProvider.java create mode 100644 samples/client/petstore/java/restclient-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ExceptionProvider.java create mode 100644 samples/client/petstore/java/restclient-springBoot4-jackson3/src/main/java/org/openapitools/client/ExceptionProvider.java create mode 100644 samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/ExceptionProvider.java create mode 100644 samples/client/petstore/java/restclient-useSingleRequestParameter-static/src/main/java/org/openapitools/client/ExceptionProvider.java create mode 100644 samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ExceptionProvider.java create mode 100644 samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/ExceptionProvider.java create mode 100644 samples/client/petstore/java/webclient-jakarta/src/main/java/org/openapitools/client/ExceptionProvider.java create mode 100644 samples/client/petstore/java/webclient-nullable-arrays/src/main/java/org/openapitools/client/ExceptionProvider.java create mode 100644 samples/client/petstore/java/webclient-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ExceptionProvider.java create mode 100644 samples/client/petstore/java/webclient-springBoot4-jackson3/src/main/java/org/openapitools/client/ExceptionProvider.java create mode 100644 samples/client/petstore/java/webclient-swagger2/src/main/java/org/openapitools/client/ExceptionProvider.java create mode 100644 samples/client/petstore/java/webclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ExceptionProvider.java create mode 100644 samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ExceptionProvider.java diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java index f779be9ed183..befc33f19e84 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/JavaClientCodegen.java @@ -720,6 +720,7 @@ public void processOpts() { // one by one for each library. supportsAdditionalPropertiesWithComposedSchema = true; } else if (libWebClient) { + supportingFiles.add(new SupportingFile("ExceptionProvider.mustache", invokerFolder, "ExceptionProvider.java")); forceSerializationLibrary(SERIALIZATION_LIBRARY_JACKSON); // Composed schemas can have the 'additionalProperties' keyword, as specified in JSON schema. @@ -729,6 +730,7 @@ public void processOpts() { // one by one for each library. supportsAdditionalPropertiesWithComposedSchema = true; } else if (libRestClient) { + supportingFiles.add(new SupportingFile("ExceptionProvider.mustache", invokerFolder, "ExceptionProvider.java")); forceSerializationLibrary(SERIALIZATION_LIBRARY_JACKSON); applyJakartaPackage(); diff --git a/modules/openapi-generator/src/main/resources/Java/ExceptionProvider.mustache b/modules/openapi-generator/src/main/resources/Java/ExceptionProvider.mustache new file mode 100644 index 000000000000..81ad9020e2f4 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/Java/ExceptionProvider.mustache @@ -0,0 +1,68 @@ +{{>licenseInfo}} + +package {{invokerPackage}}; + +import org.springframework.web.client.RestClientException; +import java.text.ParseException; + +/** + * Extension point for customizing exceptions thrown by the generated API client. + *

+ * The default implementation throws {@link RuntimeException}. To use custom exception + * types, implement this interface and pass the instance to + * {@link ApiClient#setExceptionProvider(ExceptionProvider)}. + *

+ *
{@code
+ * apiClient.setExceptionProvider(new ExceptionProvider() {
+ *     public RuntimeException bearerAuthException() {
+ *         return new MyAuthException("No Bearer authentication configured!");
+ *     }
+ * });
+ * }
+ */ +public interface ExceptionProvider { + + /** Returns an exception indicating that no Bearer authentication is configured. */ + default RuntimeException bearerAuthException() { + return new RuntimeException("No Bearer authentication configured!"); + } + + /** Returns an exception indicating that no HTTP basic authentication is configured. */ + default RuntimeException httpBasicAuthException() { + return new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Returns an exception indicating that no API key authentication is configured. */ + default RuntimeException apiKeyAuthException() { + return new RuntimeException("No API key authentication configured!"); + } + + /** Returns an exception indicating that no OAuth2 authentication is configured. */ + default RuntimeException oAuth2Exception() { + return new RuntimeException("No OAuth2 authentication configured!"); + } + + /** Wraps a date/time parse exception. */ + default RuntimeException dateTimeException(ParseException e) { + return new RuntimeException(e); + } + + /** Wraps a Jackson serialization/deserialization exception. */ + default RuntimeException jacksonException(Exception e) { + return new RuntimeException(e); + } + + /** + * Returns an exception indicating that the requested authentication scheme is not configured. + * + * @param authName the name of the authentication scheme that could not be found + * @return a {@link RestClientException} describing the missing authentication + */ + default RestClientException undefinedAuthenticationException(String authName) { + return new RestClientException("Authentication undefined: " + authName); + } + + /** The default {@link ExceptionProvider} instance. */ + ExceptionProvider DEFAULT = new ExceptionProvider() {}; + +} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/restclient/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/restclient/ApiClient.mustache index 61dac36a2540..a30bf9d25b6e 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/restclient/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/restclient/ApiClient.mustache @@ -149,6 +149,13 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { {{/useJackson3}} protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this(null); @@ -351,6 +358,25 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -372,7 +398,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -387,7 +413,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -401,7 +427,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -415,7 +441,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -429,7 +455,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -443,7 +469,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } {{#hasOAuthMethods}} @@ -468,7 +494,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { return this; } } - throw new RuntimeException("No OAuth2 authentication configured!"); + throw exceptionProvider.oAuth2Exception(); } {{/hasOAuthMethods}} @@ -524,7 +550,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -592,7 +618,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); } catch ({{#useJackson3}}JacksonException{{/useJackson3}}{{^useJackson3}}JsonProcessingException{{/useJackson3}} e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } @@ -601,7 +627,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { try { values.add(mapper.writeValueAsString(o)); } catch ({{#useJackson3}}JacksonException{{/useJackson3}}{{^useJackson3}}JsonProcessingException{{/useJackson3}} e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -935,7 +961,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { for (String authName : authNames) { Authentication auth = authentications.get(authName); if (auth == null) { - throw new RestClientException("Authentication undefined: " + authName); + throw exceptionProvider.undefinedAuthenticationException(authName); } auth.applyToParams(queryParams, headerParams, cookieParams); } diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache index e1df6981f724..0231dd08cf27 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/webclient/ApiClient.mustache @@ -133,6 +133,13 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this.dateFormat = createDefaultDateFormat(); @@ -287,6 +294,25 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -308,7 +334,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -322,7 +348,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -336,7 +362,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -350,7 +376,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -364,7 +390,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } {{#hasOAuthMethods}} @@ -379,7 +405,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { return; } } - throw new RuntimeException("No OAuth2 authentication configured!"); + throw exceptionProvider.oAuth2Exception(); } {{/hasOAuthMethods}} @@ -435,7 +461,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -502,8 +528,8 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { } else { try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); - } catch ({{#useJackson3}}JacksonException{{/useJackson3}}{{^useJackson3}}JsonProcessingException{{/useJackson3}} e) { - throw new RuntimeException(e); + } catch ({{#useJackson3}}JacksonException{{/useJackson3}}{{^useJackson3}}JsonProcessingException{{/useJackson3}} e) { + throw exceptionProvider.jacksonException(e); } } @@ -512,7 +538,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { try { values.add(mapper.writeValueAsString(o)); } catch ({{#useJackson3}}JacksonException{{/useJackson3}}{{^useJackson3}}JsonProcessingException{{/useJackson3}} e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -821,7 +847,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} { for (String authName : authNames) { Authentication auth = authentications.get(authName); if (auth == null) { - throw new RestClientException("Authentication undefined: " + authName); + throw exceptionProvider.undefinedAuthenticationException(authName); } auth.applyToParams(queryParams, headerParams, cookieParams); } diff --git a/samples/client/echo_api/java/restclient/.openapi-generator/FILES b/samples/client/echo_api/java/restclient/.openapi-generator/FILES index 939acb947c39..e4f10b1adfc1 100644 --- a/samples/client/echo_api/java/restclient/.openapi-generator/FILES +++ b/samples/client/echo_api/java/restclient/.openapi-generator/FILES @@ -33,6 +33,7 @@ pom.xml settings.gradle src/main/AndroidManifest.xml src/main/java/org/openapitools/client/ApiClient.java +src/main/java/org/openapitools/client/ExceptionProvider.java src/main/java/org/openapitools/client/JavaTimeFormatter.java src/main/java/org/openapitools/client/RFC3339DateFormat.java src/main/java/org/openapitools/client/RFC3339InstantDeserializer.java diff --git a/samples/client/echo_api/java/restclient/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/echo_api/java/restclient/src/main/java/org/openapitools/client/ApiClient.java index 4cd8c4f7984d..3672c5a0c2c8 100644 --- a/samples/client/echo_api/java/restclient/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/echo_api/java/restclient/src/main/java/org/openapitools/client/ApiClient.java @@ -97,6 +97,13 @@ protected String collectionToString(Collection collection) { protected final ObjectMapper mapper; protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this(null); @@ -242,6 +249,25 @@ public Map getAuthentications() { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -263,7 +289,7 @@ public void setBearerToken(String bearerToken) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -278,7 +304,7 @@ public void setBearerToken(Supplier tokenSupplier) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -292,7 +318,7 @@ public void setUsername(String username) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -306,7 +332,7 @@ public void setPassword(String password) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -320,7 +346,7 @@ public void setApiKey(String apiKey) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -334,7 +360,7 @@ public void setApiKeyPrefix(String apiKeyPrefix) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -389,7 +415,7 @@ public Date parseDate(String str) { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -457,7 +483,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } @@ -466,7 +492,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { values.add(mapper.writeValueAsString(o)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -800,7 +826,7 @@ protected void updateParamsForAuth(String[] authNames, MultiValueMap + * The default implementation throws {@link RuntimeException}. To use custom exception + * types, implement this interface and pass the instance to + * {@link ApiClient#setExceptionProvider(ExceptionProvider)}. + *

+ *
{@code
+ * apiClient.setExceptionProvider(new ExceptionProvider() {
+ *     public RuntimeException bearerAuthException() {
+ *         return new MyAuthException("No Bearer authentication configured!");
+ *     }
+ * });
+ * }
+ */ +public interface ExceptionProvider { + + /** Returns an exception indicating that no Bearer authentication is configured. */ + default RuntimeException bearerAuthException() { + return new RuntimeException("No Bearer authentication configured!"); + } + + /** Returns an exception indicating that no HTTP basic authentication is configured. */ + default RuntimeException httpBasicAuthException() { + return new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Returns an exception indicating that no API key authentication is configured. */ + default RuntimeException apiKeyAuthException() { + return new RuntimeException("No API key authentication configured!"); + } + + /** Returns an exception indicating that no OAuth2 authentication is configured. */ + default RuntimeException oAuth2Exception() { + return new RuntimeException("No OAuth2 authentication configured!"); + } + + /** Wraps a date/time parse exception. */ + default RuntimeException dateTimeException(ParseException e) { + return new RuntimeException(e); + } + + /** Wraps a Jackson serialization/deserialization exception. */ + default RuntimeException jacksonException(Exception e) { + return new RuntimeException(e); + } + + /** + * Returns an exception indicating that the requested authentication scheme is not configured. + * + * @param authName the name of the authentication scheme that could not be found + * @return a {@link RestClientException} describing the missing authentication + */ + default RestClientException undefinedAuthenticationException(String authName) { + return new RestClientException("Authentication undefined: " + authName); + } + + /** The default {@link ExceptionProvider} instance. */ + ExceptionProvider DEFAULT = new ExceptionProvider() {}; + +} \ No newline at end of file diff --git a/samples/client/others/java/restclient-enum-in-multipart/.openapi-generator/FILES b/samples/client/others/java/restclient-enum-in-multipart/.openapi-generator/FILES index 1ce4cca21cde..b19d56bcdd98 100644 --- a/samples/client/others/java/restclient-enum-in-multipart/.openapi-generator/FILES +++ b/samples/client/others/java/restclient-enum-in-multipart/.openapi-generator/FILES @@ -19,6 +19,7 @@ pom.xml settings.gradle src/main/AndroidManifest.xml src/main/java/org/openapitools/client/ApiClient.java +src/main/java/org/openapitools/client/ExceptionProvider.java src/main/java/org/openapitools/client/JavaTimeFormatter.java src/main/java/org/openapitools/client/RFC3339DateFormat.java src/main/java/org/openapitools/client/RFC3339InstantDeserializer.java diff --git a/samples/client/others/java/restclient-enum-in-multipart/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/others/java/restclient-enum-in-multipart/src/main/java/org/openapitools/client/ApiClient.java index a2d36a8ca35d..295d81237d8f 100644 --- a/samples/client/others/java/restclient-enum-in-multipart/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/others/java/restclient-enum-in-multipart/src/main/java/org/openapitools/client/ApiClient.java @@ -97,6 +97,13 @@ protected String collectionToString(Collection collection) { protected final ObjectMapper mapper; protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this(null); @@ -241,6 +248,25 @@ public Map getAuthentications() { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -262,7 +288,7 @@ public void setBearerToken(String bearerToken) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -277,7 +303,7 @@ public void setBearerToken(Supplier tokenSupplier) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -291,7 +317,7 @@ public void setUsername(String username) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -305,7 +331,7 @@ public void setPassword(String password) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -319,7 +345,7 @@ public void setApiKey(String apiKey) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -333,7 +359,7 @@ public void setApiKeyPrefix(String apiKeyPrefix) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -388,7 +414,7 @@ public Date parseDate(String str) { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -456,7 +482,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } @@ -465,7 +491,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { values.add(mapper.writeValueAsString(o)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -799,7 +825,7 @@ protected void updateParamsForAuth(String[] authNames, MultiValueMap + * The default implementation throws {@link RuntimeException}. To use custom exception + * types, implement this interface and pass the instance to + * {@link ApiClient#setExceptionProvider(ExceptionProvider)}. + *

+ *
{@code
+ * apiClient.setExceptionProvider(new ExceptionProvider() {
+ *     public RuntimeException bearerAuthException() {
+ *         return new MyAuthException("No Bearer authentication configured!");
+ *     }
+ * });
+ * }
+ */ +public interface ExceptionProvider { + + /** Returns an exception indicating that no Bearer authentication is configured. */ + default RuntimeException bearerAuthException() { + return new RuntimeException("No Bearer authentication configured!"); + } + + /** Returns an exception indicating that no HTTP basic authentication is configured. */ + default RuntimeException httpBasicAuthException() { + return new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Returns an exception indicating that no API key authentication is configured. */ + default RuntimeException apiKeyAuthException() { + return new RuntimeException("No API key authentication configured!"); + } + + /** Returns an exception indicating that no OAuth2 authentication is configured. */ + default RuntimeException oAuth2Exception() { + return new RuntimeException("No OAuth2 authentication configured!"); + } + + /** Wraps a date/time parse exception. */ + default RuntimeException dateTimeException(ParseException e) { + return new RuntimeException(e); + } + + /** Wraps a Jackson serialization/deserialization exception. */ + default RuntimeException jacksonException(Exception e) { + return new RuntimeException(e); + } + + /** + * Returns an exception indicating that the requested authentication scheme is not configured. + * + * @param authName the name of the authentication scheme that could not be found + * @return a {@link RestClientException} describing the missing authentication + */ + default RestClientException undefinedAuthenticationException(String authName) { + return new RestClientException("Authentication undefined: " + authName); + } + + /** The default {@link ExceptionProvider} instance. */ + ExceptionProvider DEFAULT = new ExceptionProvider() {}; + +} \ No newline at end of file diff --git a/samples/client/others/java/restclient-sealedInterface/.openapi-generator/FILES b/samples/client/others/java/restclient-sealedInterface/.openapi-generator/FILES index 79e1f1f075fa..d11eb14c0d55 100644 --- a/samples/client/others/java/restclient-sealedInterface/.openapi-generator/FILES +++ b/samples/client/others/java/restclient-sealedInterface/.openapi-generator/FILES @@ -38,6 +38,7 @@ pom.xml settings.gradle src/main/AndroidManifest.xml src/main/java/org/openapitools/client/ApiClient.java +src/main/java/org/openapitools/client/ExceptionProvider.java src/main/java/org/openapitools/client/JavaTimeFormatter.java src/main/java/org/openapitools/client/RFC3339DateFormat.java src/main/java/org/openapitools/client/RFC3339InstantDeserializer.java diff --git a/samples/client/others/java/restclient-sealedInterface/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/others/java/restclient-sealedInterface/src/main/java/org/openapitools/client/ApiClient.java index 463913c837d0..0539f1b276f8 100644 --- a/samples/client/others/java/restclient-sealedInterface/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/others/java/restclient-sealedInterface/src/main/java/org/openapitools/client/ApiClient.java @@ -97,6 +97,13 @@ protected String collectionToString(Collection collection) { protected final ObjectMapper mapper; protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this(null); @@ -240,6 +247,25 @@ public Map getAuthentications() { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -261,7 +287,7 @@ public void setBearerToken(String bearerToken) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -276,7 +302,7 @@ public void setBearerToken(Supplier tokenSupplier) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -290,7 +316,7 @@ public void setUsername(String username) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -304,7 +330,7 @@ public void setPassword(String password) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -318,7 +344,7 @@ public void setApiKey(String apiKey) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -332,7 +358,7 @@ public void setApiKeyPrefix(String apiKeyPrefix) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -387,7 +413,7 @@ public Date parseDate(String str) { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -455,7 +481,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } @@ -464,7 +490,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { values.add(mapper.writeValueAsString(o)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -798,7 +824,7 @@ protected void updateParamsForAuth(String[] authNames, MultiValueMap + * The default implementation throws {@link RuntimeException}. To use custom exception + * types, implement this interface and pass the instance to + * {@link ApiClient#setExceptionProvider(ExceptionProvider)}. + *

+ *
{@code
+ * apiClient.setExceptionProvider(new ExceptionProvider() {
+ *     public RuntimeException bearerAuthException() {
+ *         return new MyAuthException("No Bearer authentication configured!");
+ *     }
+ * });
+ * }
+ */ +public interface ExceptionProvider { + + /** Returns an exception indicating that no Bearer authentication is configured. */ + default RuntimeException bearerAuthException() { + return new RuntimeException("No Bearer authentication configured!"); + } + + /** Returns an exception indicating that no HTTP basic authentication is configured. */ + default RuntimeException httpBasicAuthException() { + return new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Returns an exception indicating that no API key authentication is configured. */ + default RuntimeException apiKeyAuthException() { + return new RuntimeException("No API key authentication configured!"); + } + + /** Returns an exception indicating that no OAuth2 authentication is configured. */ + default RuntimeException oAuth2Exception() { + return new RuntimeException("No OAuth2 authentication configured!"); + } + + /** Wraps a date/time parse exception. */ + default RuntimeException dateTimeException(ParseException e) { + return new RuntimeException(e); + } + + /** Wraps a Jackson serialization/deserialization exception. */ + default RuntimeException jacksonException(Exception e) { + return new RuntimeException(e); + } + + /** + * Returns an exception indicating that the requested authentication scheme is not configured. + * + * @param authName the name of the authentication scheme that could not be found + * @return a {@link RestClientException} describing the missing authentication + */ + default RestClientException undefinedAuthenticationException(String authName) { + return new RestClientException("Authentication undefined: " + authName); + } + + /** The default {@link ExceptionProvider} instance. */ + ExceptionProvider DEFAULT = new ExceptionProvider() {}; + +} \ No newline at end of file diff --git a/samples/client/others/java/restclient-useAbstractionForFiles/.openapi-generator/FILES b/samples/client/others/java/restclient-useAbstractionForFiles/.openapi-generator/FILES index 9078c63e26ab..0cc5234c597b 100644 --- a/samples/client/others/java/restclient-useAbstractionForFiles/.openapi-generator/FILES +++ b/samples/client/others/java/restclient-useAbstractionForFiles/.openapi-generator/FILES @@ -16,6 +16,7 @@ pom.xml settings.gradle src/main/AndroidManifest.xml src/main/java/org/openapitools/client/ApiClient.java +src/main/java/org/openapitools/client/ExceptionProvider.java src/main/java/org/openapitools/client/JavaTimeFormatter.java src/main/java/org/openapitools/client/RFC3339DateFormat.java src/main/java/org/openapitools/client/RFC3339InstantDeserializer.java diff --git a/samples/client/others/java/restclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/others/java/restclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java index 24004f99b7f7..379106f2da15 100644 --- a/samples/client/others/java/restclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/others/java/restclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java @@ -97,6 +97,13 @@ protected String collectionToString(Collection collection) { protected final ObjectMapper mapper; protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this(null); @@ -240,6 +247,25 @@ public Map getAuthentications() { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -261,7 +287,7 @@ public void setBearerToken(String bearerToken) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -276,7 +302,7 @@ public void setBearerToken(Supplier tokenSupplier) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -290,7 +316,7 @@ public void setUsername(String username) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -304,7 +330,7 @@ public void setPassword(String password) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -318,7 +344,7 @@ public void setApiKey(String apiKey) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -332,7 +358,7 @@ public void setApiKeyPrefix(String apiKeyPrefix) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -387,7 +413,7 @@ public Date parseDate(String str) { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -455,7 +481,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } @@ -464,7 +490,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { values.add(mapper.writeValueAsString(o)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -798,7 +824,7 @@ protected void updateParamsForAuth(String[] authNames, MultiValueMap + * The default implementation throws {@link RuntimeException}. To use custom exception + * types, implement this interface and pass the instance to + * {@link ApiClient#setExceptionProvider(ExceptionProvider)}. + *

+ *
{@code
+ * apiClient.setExceptionProvider(new ExceptionProvider() {
+ *     public RuntimeException bearerAuthException() {
+ *         return new MyAuthException("No Bearer authentication configured!");
+ *     }
+ * });
+ * }
+ */ +public interface ExceptionProvider { + + /** Returns an exception indicating that no Bearer authentication is configured. */ + default RuntimeException bearerAuthException() { + return new RuntimeException("No Bearer authentication configured!"); + } + + /** Returns an exception indicating that no HTTP basic authentication is configured. */ + default RuntimeException httpBasicAuthException() { + return new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Returns an exception indicating that no API key authentication is configured. */ + default RuntimeException apiKeyAuthException() { + return new RuntimeException("No API key authentication configured!"); + } + + /** Returns an exception indicating that no OAuth2 authentication is configured. */ + default RuntimeException oAuth2Exception() { + return new RuntimeException("No OAuth2 authentication configured!"); + } + + /** Wraps a date/time parse exception. */ + default RuntimeException dateTimeException(ParseException e) { + return new RuntimeException(e); + } + + /** Wraps a Jackson serialization/deserialization exception. */ + default RuntimeException jacksonException(Exception e) { + return new RuntimeException(e); + } + + /** + * Returns an exception indicating that the requested authentication scheme is not configured. + * + * @param authName the name of the authentication scheme that could not be found + * @return a {@link RestClientException} describing the missing authentication + */ + default RestClientException undefinedAuthenticationException(String authName) { + return new RestClientException("Authentication undefined: " + authName); + } + + /** The default {@link ExceptionProvider} instance. */ + ExceptionProvider DEFAULT = new ExceptionProvider() {}; + +} \ No newline at end of file diff --git a/samples/client/others/java/webclient-sealedInterface/.openapi-generator/FILES b/samples/client/others/java/webclient-sealedInterface/.openapi-generator/FILES index 79e1f1f075fa..d11eb14c0d55 100644 --- a/samples/client/others/java/webclient-sealedInterface/.openapi-generator/FILES +++ b/samples/client/others/java/webclient-sealedInterface/.openapi-generator/FILES @@ -38,6 +38,7 @@ pom.xml settings.gradle src/main/AndroidManifest.xml src/main/java/org/openapitools/client/ApiClient.java +src/main/java/org/openapitools/client/ExceptionProvider.java src/main/java/org/openapitools/client/JavaTimeFormatter.java src/main/java/org/openapitools/client/RFC3339DateFormat.java src/main/java/org/openapitools/client/RFC3339InstantDeserializer.java diff --git a/samples/client/others/java/webclient-sealedInterface/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/others/java/webclient-sealedInterface/src/main/java/org/openapitools/client/ApiClient.java index 425d78b6670f..04c559f1ee7b 100644 --- a/samples/client/others/java/webclient-sealedInterface/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/others/java/webclient-sealedInterface/src/main/java/org/openapitools/client/ApiClient.java @@ -109,6 +109,13 @@ protected String collectionToString(Collection collection) { protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this.dateFormat = createDefaultDateFormat(); @@ -230,6 +237,25 @@ public Map getAuthentications() { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -251,7 +277,7 @@ public void setBearerToken(String bearerToken) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -265,7 +291,7 @@ public void setUsername(String username) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -279,7 +305,7 @@ public void setPassword(String password) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -293,7 +319,7 @@ public void setApiKey(String apiKey) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -307,7 +333,7 @@ public void setApiKeyPrefix(String apiKeyPrefix) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -362,7 +388,7 @@ public Date parseDate(String str) { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -429,8 +455,8 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm } else { try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + throw exceptionProvider.jacksonException(e); } } @@ -439,7 +465,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { values.add(mapper.writeValueAsString(o)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -743,7 +769,7 @@ protected void updateParamsForAuth(String[] authNames, MultiValueMap + * The default implementation throws {@link RuntimeException}. To use custom exception + * types, implement this interface and pass the instance to + * {@link ApiClient#setExceptionProvider(ExceptionProvider)}. + *

+ *
{@code
+ * apiClient.setExceptionProvider(new ExceptionProvider() {
+ *     public RuntimeException bearerAuthException() {
+ *         return new MyAuthException("No Bearer authentication configured!");
+ *     }
+ * });
+ * }
+ */ +public interface ExceptionProvider { + + /** Returns an exception indicating that no Bearer authentication is configured. */ + default RuntimeException bearerAuthException() { + return new RuntimeException("No Bearer authentication configured!"); + } + + /** Returns an exception indicating that no HTTP basic authentication is configured. */ + default RuntimeException httpBasicAuthException() { + return new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Returns an exception indicating that no API key authentication is configured. */ + default RuntimeException apiKeyAuthException() { + return new RuntimeException("No API key authentication configured!"); + } + + /** Returns an exception indicating that no OAuth2 authentication is configured. */ + default RuntimeException oAuth2Exception() { + return new RuntimeException("No OAuth2 authentication configured!"); + } + + /** Wraps a date/time parse exception. */ + default RuntimeException dateTimeException(ParseException e) { + return new RuntimeException(e); + } + + /** Wraps a Jackson serialization/deserialization exception. */ + default RuntimeException jacksonException(Exception e) { + return new RuntimeException(e); + } + + /** + * Returns an exception indicating that the requested authentication scheme is not configured. + * + * @param authName the name of the authentication scheme that could not be found + * @return a {@link RestClientException} describing the missing authentication + */ + default RestClientException undefinedAuthenticationException(String authName) { + return new RestClientException("Authentication undefined: " + authName); + } + + /** The default {@link ExceptionProvider} instance. */ + ExceptionProvider DEFAULT = new ExceptionProvider() {}; + +} \ No newline at end of file diff --git a/samples/client/others/java/webclient-sealedInterface_3_1/.openapi-generator/FILES b/samples/client/others/java/webclient-sealedInterface_3_1/.openapi-generator/FILES index 79e1f1f075fa..d11eb14c0d55 100644 --- a/samples/client/others/java/webclient-sealedInterface_3_1/.openapi-generator/FILES +++ b/samples/client/others/java/webclient-sealedInterface_3_1/.openapi-generator/FILES @@ -38,6 +38,7 @@ pom.xml settings.gradle src/main/AndroidManifest.xml src/main/java/org/openapitools/client/ApiClient.java +src/main/java/org/openapitools/client/ExceptionProvider.java src/main/java/org/openapitools/client/JavaTimeFormatter.java src/main/java/org/openapitools/client/RFC3339DateFormat.java src/main/java/org/openapitools/client/RFC3339InstantDeserializer.java diff --git a/samples/client/others/java/webclient-sealedInterface_3_1/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/others/java/webclient-sealedInterface_3_1/src/main/java/org/openapitools/client/ApiClient.java index 425d78b6670f..04c559f1ee7b 100644 --- a/samples/client/others/java/webclient-sealedInterface_3_1/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/others/java/webclient-sealedInterface_3_1/src/main/java/org/openapitools/client/ApiClient.java @@ -109,6 +109,13 @@ protected String collectionToString(Collection collection) { protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this.dateFormat = createDefaultDateFormat(); @@ -230,6 +237,25 @@ public Map getAuthentications() { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -251,7 +277,7 @@ public void setBearerToken(String bearerToken) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -265,7 +291,7 @@ public void setUsername(String username) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -279,7 +305,7 @@ public void setPassword(String password) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -293,7 +319,7 @@ public void setApiKey(String apiKey) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -307,7 +333,7 @@ public void setApiKeyPrefix(String apiKeyPrefix) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -362,7 +388,7 @@ public Date parseDate(String str) { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -429,8 +455,8 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm } else { try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + throw exceptionProvider.jacksonException(e); } } @@ -439,7 +465,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { values.add(mapper.writeValueAsString(o)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -743,7 +769,7 @@ protected void updateParamsForAuth(String[] authNames, MultiValueMap + * The default implementation throws {@link RuntimeException}. To use custom exception + * types, implement this interface and pass the instance to + * {@link ApiClient#setExceptionProvider(ExceptionProvider)}. + *

+ *
{@code
+ * apiClient.setExceptionProvider(new ExceptionProvider() {
+ *     public RuntimeException bearerAuthException() {
+ *         return new MyAuthException("No Bearer authentication configured!");
+ *     }
+ * });
+ * }
+ */ +public interface ExceptionProvider { + + /** Returns an exception indicating that no Bearer authentication is configured. */ + default RuntimeException bearerAuthException() { + return new RuntimeException("No Bearer authentication configured!"); + } + + /** Returns an exception indicating that no HTTP basic authentication is configured. */ + default RuntimeException httpBasicAuthException() { + return new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Returns an exception indicating that no API key authentication is configured. */ + default RuntimeException apiKeyAuthException() { + return new RuntimeException("No API key authentication configured!"); + } + + /** Returns an exception indicating that no OAuth2 authentication is configured. */ + default RuntimeException oAuth2Exception() { + return new RuntimeException("No OAuth2 authentication configured!"); + } + + /** Wraps a date/time parse exception. */ + default RuntimeException dateTimeException(ParseException e) { + return new RuntimeException(e); + } + + /** Wraps a Jackson serialization/deserialization exception. */ + default RuntimeException jacksonException(Exception e) { + return new RuntimeException(e); + } + + /** + * Returns an exception indicating that the requested authentication scheme is not configured. + * + * @param authName the name of the authentication scheme that could not be found + * @return a {@link RestClientException} describing the missing authentication + */ + default RestClientException undefinedAuthenticationException(String authName) { + return new RestClientException("Authentication undefined: " + authName); + } + + /** The default {@link ExceptionProvider} instance. */ + ExceptionProvider DEFAULT = new ExceptionProvider() {}; + +} \ No newline at end of file diff --git a/samples/client/others/java/webclient-useAbstractionForFiles/.openapi-generator/FILES b/samples/client/others/java/webclient-useAbstractionForFiles/.openapi-generator/FILES index 9078c63e26ab..0cc5234c597b 100644 --- a/samples/client/others/java/webclient-useAbstractionForFiles/.openapi-generator/FILES +++ b/samples/client/others/java/webclient-useAbstractionForFiles/.openapi-generator/FILES @@ -16,6 +16,7 @@ pom.xml settings.gradle src/main/AndroidManifest.xml src/main/java/org/openapitools/client/ApiClient.java +src/main/java/org/openapitools/client/ExceptionProvider.java src/main/java/org/openapitools/client/JavaTimeFormatter.java src/main/java/org/openapitools/client/RFC3339DateFormat.java src/main/java/org/openapitools/client/RFC3339InstantDeserializer.java diff --git a/samples/client/others/java/webclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/others/java/webclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java index 1fde9de1cf79..bb6d06a0a2f9 100644 --- a/samples/client/others/java/webclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/others/java/webclient-useAbstractionForFiles/src/main/java/org/openapitools/client/ApiClient.java @@ -109,6 +109,13 @@ protected String collectionToString(Collection collection) { protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this.dateFormat = createDefaultDateFormat(); @@ -230,6 +237,25 @@ public Map getAuthentications() { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -251,7 +277,7 @@ public void setBearerToken(String bearerToken) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -265,7 +291,7 @@ public void setUsername(String username) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -279,7 +305,7 @@ public void setPassword(String password) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -293,7 +319,7 @@ public void setApiKey(String apiKey) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -307,7 +333,7 @@ public void setApiKeyPrefix(String apiKeyPrefix) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -362,7 +388,7 @@ public Date parseDate(String str) { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -429,8 +455,8 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm } else { try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + throw exceptionProvider.jacksonException(e); } } @@ -439,7 +465,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { values.add(mapper.writeValueAsString(o)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -743,7 +769,7 @@ protected void updateParamsForAuth(String[] authNames, MultiValueMap + * The default implementation throws {@link RuntimeException}. To use custom exception + * types, implement this interface and pass the instance to + * {@link ApiClient#setExceptionProvider(ExceptionProvider)}. + *

+ *
{@code
+ * apiClient.setExceptionProvider(new ExceptionProvider() {
+ *     public RuntimeException bearerAuthException() {
+ *         return new MyAuthException("No Bearer authentication configured!");
+ *     }
+ * });
+ * }
+ */ +public interface ExceptionProvider { + + /** Returns an exception indicating that no Bearer authentication is configured. */ + default RuntimeException bearerAuthException() { + return new RuntimeException("No Bearer authentication configured!"); + } + + /** Returns an exception indicating that no HTTP basic authentication is configured. */ + default RuntimeException httpBasicAuthException() { + return new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Returns an exception indicating that no API key authentication is configured. */ + default RuntimeException apiKeyAuthException() { + return new RuntimeException("No API key authentication configured!"); + } + + /** Returns an exception indicating that no OAuth2 authentication is configured. */ + default RuntimeException oAuth2Exception() { + return new RuntimeException("No OAuth2 authentication configured!"); + } + + /** Wraps a date/time parse exception. */ + default RuntimeException dateTimeException(ParseException e) { + return new RuntimeException(e); + } + + /** Wraps a Jackson serialization/deserialization exception. */ + default RuntimeException jacksonException(Exception e) { + return new RuntimeException(e); + } + + /** + * Returns an exception indicating that the requested authentication scheme is not configured. + * + * @param authName the name of the authentication scheme that could not be found + * @return a {@link RestClientException} describing the missing authentication + */ + default RestClientException undefinedAuthenticationException(String authName) { + return new RestClientException("Authentication undefined: " + authName); + } + + /** The default {@link ExceptionProvider} instance. */ + ExceptionProvider DEFAULT = new ExceptionProvider() {}; + +} \ No newline at end of file diff --git a/samples/client/petstore/java/restclient-nullable-arrays/.openapi-generator/FILES b/samples/client/petstore/java/restclient-nullable-arrays/.openapi-generator/FILES index eedafa945afd..1466503e3b79 100644 --- a/samples/client/petstore/java/restclient-nullable-arrays/.openapi-generator/FILES +++ b/samples/client/petstore/java/restclient-nullable-arrays/.openapi-generator/FILES @@ -17,6 +17,7 @@ pom.xml settings.gradle src/main/AndroidManifest.xml src/main/java/org/openapitools/client/ApiClient.java +src/main/java/org/openapitools/client/ExceptionProvider.java src/main/java/org/openapitools/client/JavaTimeFormatter.java src/main/java/org/openapitools/client/RFC3339DateFormat.java src/main/java/org/openapitools/client/RFC3339InstantDeserializer.java diff --git a/samples/client/petstore/java/restclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java index f70c16ca989a..ac65442c5556 100644 --- a/samples/client/petstore/java/restclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java @@ -97,6 +97,13 @@ protected String collectionToString(Collection collection) { protected final ObjectMapper mapper; protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this(null); @@ -240,6 +247,25 @@ public Map getAuthentications() { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -261,7 +287,7 @@ public void setBearerToken(String bearerToken) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -276,7 +302,7 @@ public void setBearerToken(Supplier tokenSupplier) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -290,7 +316,7 @@ public void setUsername(String username) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -304,7 +330,7 @@ public void setPassword(String password) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -318,7 +344,7 @@ public void setApiKey(String apiKey) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -332,7 +358,7 @@ public void setApiKeyPrefix(String apiKeyPrefix) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -387,7 +413,7 @@ public Date parseDate(String str) { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -455,7 +481,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } @@ -464,7 +490,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { values.add(mapper.writeValueAsString(o)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -798,7 +824,7 @@ protected void updateParamsForAuth(String[] authNames, MultiValueMap + * The default implementation throws {@link RuntimeException}. To use custom exception + * types, implement this interface and pass the instance to + * {@link ApiClient#setExceptionProvider(ExceptionProvider)}. + *

+ *
{@code
+ * apiClient.setExceptionProvider(new ExceptionProvider() {
+ *     public RuntimeException bearerAuthException() {
+ *         return new MyAuthException("No Bearer authentication configured!");
+ *     }
+ * });
+ * }
+ */ +public interface ExceptionProvider { + + /** Returns an exception indicating that no Bearer authentication is configured. */ + default RuntimeException bearerAuthException() { + return new RuntimeException("No Bearer authentication configured!"); + } + + /** Returns an exception indicating that no HTTP basic authentication is configured. */ + default RuntimeException httpBasicAuthException() { + return new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Returns an exception indicating that no API key authentication is configured. */ + default RuntimeException apiKeyAuthException() { + return new RuntimeException("No API key authentication configured!"); + } + + /** Returns an exception indicating that no OAuth2 authentication is configured. */ + default RuntimeException oAuth2Exception() { + return new RuntimeException("No OAuth2 authentication configured!"); + } + + /** Wraps a date/time parse exception. */ + default RuntimeException dateTimeException(ParseException e) { + return new RuntimeException(e); + } + + /** Wraps a Jackson serialization/deserialization exception. */ + default RuntimeException jacksonException(Exception e) { + return new RuntimeException(e); + } + + /** + * Returns an exception indicating that the requested authentication scheme is not configured. + * + * @param authName the name of the authentication scheme that could not be found + * @return a {@link RestClientException} describing the missing authentication + */ + default RestClientException undefinedAuthenticationException(String authName) { + return new RestClientException("Authentication undefined: " + authName); + } + + /** The default {@link ExceptionProvider} instance. */ + ExceptionProvider DEFAULT = new ExceptionProvider() {}; + +} \ No newline at end of file diff --git a/samples/client/petstore/java/restclient-springBoot4-jackson2/.openapi-generator/FILES b/samples/client/petstore/java/restclient-springBoot4-jackson2/.openapi-generator/FILES index 345da529f8a1..94cc6f4676ac 100644 --- a/samples/client/petstore/java/restclient-springBoot4-jackson2/.openapi-generator/FILES +++ b/samples/client/petstore/java/restclient-springBoot4-jackson2/.openapi-generator/FILES @@ -19,6 +19,7 @@ pom.xml settings.gradle src/main/AndroidManifest.xml src/main/java/org/openapitools/client/ApiClient.java +src/main/java/org/openapitools/client/ExceptionProvider.java src/main/java/org/openapitools/client/JavaTimeFormatter.java src/main/java/org/openapitools/client/RFC3339DateFormat.java src/main/java/org/openapitools/client/RFC3339InstantDeserializer.java diff --git a/samples/client/petstore/java/restclient-springBoot4-jackson2/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-springBoot4-jackson2/src/main/java/org/openapitools/client/ApiClient.java index ee5a0f33e77d..da6ffdc29d6b 100644 --- a/samples/client/petstore/java/restclient-springBoot4-jackson2/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-springBoot4-jackson2/src/main/java/org/openapitools/client/ApiClient.java @@ -98,6 +98,13 @@ protected String collectionToString(Collection collection) { protected final ObjectMapper mapper; protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this(null); @@ -242,6 +249,25 @@ public Map getAuthentications() { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -263,7 +289,7 @@ public void setBearerToken(String bearerToken) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -278,7 +304,7 @@ public void setBearerToken(Supplier tokenSupplier) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -292,7 +318,7 @@ public void setUsername(String username) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -306,7 +332,7 @@ public void setPassword(String password) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -320,7 +346,7 @@ public void setApiKey(String apiKey) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -334,7 +360,7 @@ public void setApiKeyPrefix(String apiKeyPrefix) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -358,7 +384,7 @@ public ApiClient setAccessToken(Supplier tokenSupplier) { return this; } } - throw new RuntimeException("No OAuth2 authentication configured!"); + throw exceptionProvider.oAuth2Exception(); } /** @@ -413,7 +439,7 @@ public Date parseDate(String str) { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -481,7 +507,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } @@ -490,7 +516,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { values.add(mapper.writeValueAsString(o)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -824,7 +850,7 @@ protected void updateParamsForAuth(String[] authNames, MultiValueMap + * The default implementation throws {@link RuntimeException}. To use custom exception + * types, implement this interface and pass the instance to + * {@link ApiClient#setExceptionProvider(ExceptionProvider)}. + *

+ *
{@code
+ * apiClient.setExceptionProvider(new ExceptionProvider() {
+ *     public RuntimeException bearerAuthException() {
+ *         return new MyAuthException("No Bearer authentication configured!");
+ *     }
+ * });
+ * }
+ */ +public interface ExceptionProvider { + + /** Returns an exception indicating that no Bearer authentication is configured. */ + default RuntimeException bearerAuthException() { + return new RuntimeException("No Bearer authentication configured!"); + } + + /** Returns an exception indicating that no HTTP basic authentication is configured. */ + default RuntimeException httpBasicAuthException() { + return new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Returns an exception indicating that no API key authentication is configured. */ + default RuntimeException apiKeyAuthException() { + return new RuntimeException("No API key authentication configured!"); + } + + /** Returns an exception indicating that no OAuth2 authentication is configured. */ + default RuntimeException oAuth2Exception() { + return new RuntimeException("No OAuth2 authentication configured!"); + } + + /** Wraps a date/time parse exception. */ + default RuntimeException dateTimeException(ParseException e) { + return new RuntimeException(e); + } + + /** Wraps a Jackson serialization/deserialization exception. */ + default RuntimeException jacksonException(Exception e) { + return new RuntimeException(e); + } + + /** + * Returns an exception indicating that the requested authentication scheme is not configured. + * + * @param authName the name of the authentication scheme that could not be found + * @return a {@link RestClientException} describing the missing authentication + */ + default RestClientException undefinedAuthenticationException(String authName) { + return new RestClientException("Authentication undefined: " + authName); + } + + /** The default {@link ExceptionProvider} instance. */ + ExceptionProvider DEFAULT = new ExceptionProvider() {}; + +} \ No newline at end of file diff --git a/samples/client/petstore/java/restclient-springBoot4-jackson3-jspecify/.openapi-generator/FILES b/samples/client/petstore/java/restclient-springBoot4-jackson3-jspecify/.openapi-generator/FILES index 399326b1defb..0360f20c7340 100644 --- a/samples/client/petstore/java/restclient-springBoot4-jackson3-jspecify/.openapi-generator/FILES +++ b/samples/client/petstore/java/restclient-springBoot4-jackson3-jspecify/.openapi-generator/FILES @@ -17,6 +17,7 @@ pom.xml settings.gradle src/main/AndroidManifest.xml src/main/java/org/openapitools/client/ApiClient.java +src/main/java/org/openapitools/client/ExceptionProvider.java src/main/java/org/openapitools/client/JavaTimeFormatter.java src/main/java/org/openapitools/client/RFC3339DateFormat.java src/main/java/org/openapitools/client/ServerConfiguration.java diff --git a/samples/client/petstore/java/restclient-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ApiClient.java index 4ca008ddb39b..b746bc9ca3e8 100644 --- a/samples/client/petstore/java/restclient-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ApiClient.java @@ -96,6 +96,13 @@ protected String collectionToString(Collection collection) { protected final JsonMapper mapper; protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this(null); @@ -233,6 +240,25 @@ public Map getAuthentications() { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -254,7 +280,7 @@ public void setBearerToken(String bearerToken) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -269,7 +295,7 @@ public void setBearerToken(Supplier tokenSupplier) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -283,7 +309,7 @@ public void setUsername(String username) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -297,7 +323,7 @@ public void setPassword(String password) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -311,7 +337,7 @@ public void setApiKey(String apiKey) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -325,7 +351,7 @@ public void setApiKeyPrefix(String apiKeyPrefix) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -380,7 +406,7 @@ public Date parseDate(String str) { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -448,7 +474,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); } catch (JacksonException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } @@ -457,7 +483,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { values.add(mapper.writeValueAsString(o)); } catch (JacksonException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -791,7 +817,7 @@ protected void updateParamsForAuth(String[] authNames, MultiValueMap + * The default implementation throws {@link RuntimeException}. To use custom exception + * types, implement this interface and pass the instance to + * {@link ApiClient#setExceptionProvider(ExceptionProvider)}. + *

+ *
{@code
+ * apiClient.setExceptionProvider(new ExceptionProvider() {
+ *     public RuntimeException bearerAuthException() {
+ *         return new MyAuthException("No Bearer authentication configured!");
+ *     }
+ * });
+ * }
+ */ +public interface ExceptionProvider { + + /** Returns an exception indicating that no Bearer authentication is configured. */ + default RuntimeException bearerAuthException() { + return new RuntimeException("No Bearer authentication configured!"); + } + + /** Returns an exception indicating that no HTTP basic authentication is configured. */ + default RuntimeException httpBasicAuthException() { + return new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Returns an exception indicating that no API key authentication is configured. */ + default RuntimeException apiKeyAuthException() { + return new RuntimeException("No API key authentication configured!"); + } + + /** Returns an exception indicating that no OAuth2 authentication is configured. */ + default RuntimeException oAuth2Exception() { + return new RuntimeException("No OAuth2 authentication configured!"); + } + + /** Wraps a date/time parse exception. */ + default RuntimeException dateTimeException(ParseException e) { + return new RuntimeException(e); + } + + /** Wraps a Jackson serialization/deserialization exception. */ + default RuntimeException jacksonException(Exception e) { + return new RuntimeException(e); + } + + /** + * Returns an exception indicating that the requested authentication scheme is not configured. + * + * @param authName the name of the authentication scheme that could not be found + * @return a {@link RestClientException} describing the missing authentication + */ + default RestClientException undefinedAuthenticationException(String authName) { + return new RestClientException("Authentication undefined: " + authName); + } + + /** The default {@link ExceptionProvider} instance. */ + ExceptionProvider DEFAULT = new ExceptionProvider() {}; + +} \ No newline at end of file diff --git a/samples/client/petstore/java/restclient-springBoot4-jackson3/.openapi-generator/FILES b/samples/client/petstore/java/restclient-springBoot4-jackson3/.openapi-generator/FILES index 389fe62eaae9..9676ccd030da 100644 --- a/samples/client/petstore/java/restclient-springBoot4-jackson3/.openapi-generator/FILES +++ b/samples/client/petstore/java/restclient-springBoot4-jackson3/.openapi-generator/FILES @@ -19,6 +19,7 @@ pom.xml settings.gradle src/main/AndroidManifest.xml src/main/java/org/openapitools/client/ApiClient.java +src/main/java/org/openapitools/client/ExceptionProvider.java src/main/java/org/openapitools/client/JavaTimeFormatter.java src/main/java/org/openapitools/client/RFC3339DateFormat.java src/main/java/org/openapitools/client/ServerConfiguration.java diff --git a/samples/client/petstore/java/restclient-springBoot4-jackson3/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-springBoot4-jackson3/src/main/java/org/openapitools/client/ApiClient.java index b521b16abd32..ff214cba8b61 100644 --- a/samples/client/petstore/java/restclient-springBoot4-jackson3/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-springBoot4-jackson3/src/main/java/org/openapitools/client/ApiClient.java @@ -97,6 +97,13 @@ protected String collectionToString(Collection collection) { protected final JsonMapper mapper; protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this(null); @@ -235,6 +242,25 @@ public Map getAuthentications() { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -256,7 +282,7 @@ public void setBearerToken(String bearerToken) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -271,7 +297,7 @@ public void setBearerToken(Supplier tokenSupplier) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -285,7 +311,7 @@ public void setUsername(String username) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -299,7 +325,7 @@ public void setPassword(String password) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -313,7 +339,7 @@ public void setApiKey(String apiKey) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -327,7 +353,7 @@ public void setApiKeyPrefix(String apiKeyPrefix) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -351,7 +377,7 @@ public ApiClient setAccessToken(Supplier tokenSupplier) { return this; } } - throw new RuntimeException("No OAuth2 authentication configured!"); + throw exceptionProvider.oAuth2Exception(); } /** @@ -406,7 +432,7 @@ public Date parseDate(String str) { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -474,7 +500,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); } catch (JacksonException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } @@ -483,7 +509,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { values.add(mapper.writeValueAsString(o)); } catch (JacksonException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -817,7 +843,7 @@ protected void updateParamsForAuth(String[] authNames, MultiValueMap + * The default implementation throws {@link RuntimeException}. To use custom exception + * types, implement this interface and pass the instance to + * {@link ApiClient#setExceptionProvider(ExceptionProvider)}. + *

+ *
{@code
+ * apiClient.setExceptionProvider(new ExceptionProvider() {
+ *     public RuntimeException bearerAuthException() {
+ *         return new MyAuthException("No Bearer authentication configured!");
+ *     }
+ * });
+ * }
+ */ +public interface ExceptionProvider { + + /** Returns an exception indicating that no Bearer authentication is configured. */ + default RuntimeException bearerAuthException() { + return new RuntimeException("No Bearer authentication configured!"); + } + + /** Returns an exception indicating that no HTTP basic authentication is configured. */ + default RuntimeException httpBasicAuthException() { + return new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Returns an exception indicating that no API key authentication is configured. */ + default RuntimeException apiKeyAuthException() { + return new RuntimeException("No API key authentication configured!"); + } + + /** Returns an exception indicating that no OAuth2 authentication is configured. */ + default RuntimeException oAuth2Exception() { + return new RuntimeException("No OAuth2 authentication configured!"); + } + + /** Wraps a date/time parse exception. */ + default RuntimeException dateTimeException(ParseException e) { + return new RuntimeException(e); + } + + /** Wraps a Jackson serialization/deserialization exception. */ + default RuntimeException jacksonException(Exception e) { + return new RuntimeException(e); + } + + /** + * Returns an exception indicating that the requested authentication scheme is not configured. + * + * @param authName the name of the authentication scheme that could not be found + * @return a {@link RestClientException} describing the missing authentication + */ + default RestClientException undefinedAuthenticationException(String authName) { + return new RestClientException("Authentication undefined: " + authName); + } + + /** The default {@link ExceptionProvider} instance. */ + ExceptionProvider DEFAULT = new ExceptionProvider() {}; + +} \ No newline at end of file diff --git a/samples/client/petstore/java/restclient-swagger2/.openapi-generator/FILES b/samples/client/petstore/java/restclient-swagger2/.openapi-generator/FILES index 555600856098..d7a6d76bf38f 100644 --- a/samples/client/petstore/java/restclient-swagger2/.openapi-generator/FILES +++ b/samples/client/petstore/java/restclient-swagger2/.openapi-generator/FILES @@ -72,6 +72,7 @@ pom.xml settings.gradle src/main/AndroidManifest.xml src/main/java/org/openapitools/client/ApiClient.java +src/main/java/org/openapitools/client/ExceptionProvider.java src/main/java/org/openapitools/client/JavaTimeFormatter.java src/main/java/org/openapitools/client/RFC3339DateFormat.java src/main/java/org/openapitools/client/RFC3339InstantDeserializer.java diff --git a/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java index f773ee249976..ba9fc446b26a 100644 --- a/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java @@ -141,6 +141,13 @@ protected String collectionToString(Collection collection) { protected final ObjectMapper mapper; protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this(null); @@ -289,6 +296,25 @@ public Map getAuthentications() { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -310,7 +336,7 @@ public void setBearerToken(String bearerToken) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -325,7 +351,7 @@ public void setBearerToken(Supplier tokenSupplier) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -339,7 +365,7 @@ public void setUsername(String username) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -353,7 +379,7 @@ public void setPassword(String password) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -367,7 +393,7 @@ public void setApiKey(String apiKey) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -381,7 +407,7 @@ public void setApiKeyPrefix(String apiKeyPrefix) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -405,7 +431,7 @@ public ApiClient setAccessToken(Supplier tokenSupplier) { return this; } } - throw new RuntimeException("No OAuth2 authentication configured!"); + throw exceptionProvider.oAuth2Exception(); } /** @@ -460,7 +486,7 @@ public Date parseDate(String str) { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -528,7 +554,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } @@ -537,7 +563,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { values.add(mapper.writeValueAsString(o)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -871,7 +897,7 @@ protected void updateParamsForAuth(String[] authNames, MultiValueMap + * The default implementation throws {@link RuntimeException}. To use custom exception + * types, implement this interface and pass the instance to + * {@link ApiClient#setExceptionProvider(ExceptionProvider)}. + *

+ *
{@code
+ * apiClient.setExceptionProvider(new ExceptionProvider() {
+ *     public RuntimeException bearerAuthException() {
+ *         return new MyAuthException("No Bearer authentication configured!");
+ *     }
+ * });
+ * }
+ */ +public interface ExceptionProvider { + + /** Returns an exception indicating that no Bearer authentication is configured. */ + default RuntimeException bearerAuthException() { + return new RuntimeException("No Bearer authentication configured!"); + } + + /** Returns an exception indicating that no HTTP basic authentication is configured. */ + default RuntimeException httpBasicAuthException() { + return new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Returns an exception indicating that no API key authentication is configured. */ + default RuntimeException apiKeyAuthException() { + return new RuntimeException("No API key authentication configured!"); + } + + /** Returns an exception indicating that no OAuth2 authentication is configured. */ + default RuntimeException oAuth2Exception() { + return new RuntimeException("No OAuth2 authentication configured!"); + } + + /** Wraps a date/time parse exception. */ + default RuntimeException dateTimeException(ParseException e) { + return new RuntimeException(e); + } + + /** Wraps a Jackson serialization/deserialization exception. */ + default RuntimeException jacksonException(Exception e) { + return new RuntimeException(e); + } + + /** + * Returns an exception indicating that the requested authentication scheme is not configured. + * + * @param authName the name of the authentication scheme that could not be found + * @return a {@link RestClientException} describing the missing authentication + */ + default RestClientException undefinedAuthenticationException(String authName) { + return new RestClientException("Authentication undefined: " + authName); + } + + /** The default {@link ExceptionProvider} instance. */ + ExceptionProvider DEFAULT = new ExceptionProvider() {}; + +} \ No newline at end of file diff --git a/samples/client/petstore/java/restclient-useSingleRequestParameter-static/.openapi-generator/FILES b/samples/client/petstore/java/restclient-useSingleRequestParameter-static/.openapi-generator/FILES index 555600856098..d7a6d76bf38f 100644 --- a/samples/client/petstore/java/restclient-useSingleRequestParameter-static/.openapi-generator/FILES +++ b/samples/client/petstore/java/restclient-useSingleRequestParameter-static/.openapi-generator/FILES @@ -72,6 +72,7 @@ pom.xml settings.gradle src/main/AndroidManifest.xml src/main/java/org/openapitools/client/ApiClient.java +src/main/java/org/openapitools/client/ExceptionProvider.java src/main/java/org/openapitools/client/JavaTimeFormatter.java src/main/java/org/openapitools/client/RFC3339DateFormat.java src/main/java/org/openapitools/client/RFC3339InstantDeserializer.java diff --git a/samples/client/petstore/java/restclient-useSingleRequestParameter-static/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-useSingleRequestParameter-static/src/main/java/org/openapitools/client/ApiClient.java index f773ee249976..ba9fc446b26a 100644 --- a/samples/client/petstore/java/restclient-useSingleRequestParameter-static/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-useSingleRequestParameter-static/src/main/java/org/openapitools/client/ApiClient.java @@ -141,6 +141,13 @@ protected String collectionToString(Collection collection) { protected final ObjectMapper mapper; protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this(null); @@ -289,6 +296,25 @@ public Map getAuthentications() { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -310,7 +336,7 @@ public void setBearerToken(String bearerToken) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -325,7 +351,7 @@ public void setBearerToken(Supplier tokenSupplier) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -339,7 +365,7 @@ public void setUsername(String username) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -353,7 +379,7 @@ public void setPassword(String password) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -367,7 +393,7 @@ public void setApiKey(String apiKey) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -381,7 +407,7 @@ public void setApiKeyPrefix(String apiKeyPrefix) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -405,7 +431,7 @@ public ApiClient setAccessToken(Supplier tokenSupplier) { return this; } } - throw new RuntimeException("No OAuth2 authentication configured!"); + throw exceptionProvider.oAuth2Exception(); } /** @@ -460,7 +486,7 @@ public Date parseDate(String str) { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -528,7 +554,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } @@ -537,7 +563,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { values.add(mapper.writeValueAsString(o)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -871,7 +897,7 @@ protected void updateParamsForAuth(String[] authNames, MultiValueMap + * The default implementation throws {@link RuntimeException}. To use custom exception + * types, implement this interface and pass the instance to + * {@link ApiClient#setExceptionProvider(ExceptionProvider)}. + *

+ *
{@code
+ * apiClient.setExceptionProvider(new ExceptionProvider() {
+ *     public RuntimeException bearerAuthException() {
+ *         return new MyAuthException("No Bearer authentication configured!");
+ *     }
+ * });
+ * }
+ */ +public interface ExceptionProvider { + + /** Returns an exception indicating that no Bearer authentication is configured. */ + default RuntimeException bearerAuthException() { + return new RuntimeException("No Bearer authentication configured!"); + } + + /** Returns an exception indicating that no HTTP basic authentication is configured. */ + default RuntimeException httpBasicAuthException() { + return new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Returns an exception indicating that no API key authentication is configured. */ + default RuntimeException apiKeyAuthException() { + return new RuntimeException("No API key authentication configured!"); + } + + /** Returns an exception indicating that no OAuth2 authentication is configured. */ + default RuntimeException oAuth2Exception() { + return new RuntimeException("No OAuth2 authentication configured!"); + } + + /** Wraps a date/time parse exception. */ + default RuntimeException dateTimeException(ParseException e) { + return new RuntimeException(e); + } + + /** Wraps a Jackson serialization/deserialization exception. */ + default RuntimeException jacksonException(Exception e) { + return new RuntimeException(e); + } + + /** + * Returns an exception indicating that the requested authentication scheme is not configured. + * + * @param authName the name of the authentication scheme that could not be found + * @return a {@link RestClientException} describing the missing authentication + */ + default RestClientException undefinedAuthenticationException(String authName) { + return new RestClientException("Authentication undefined: " + authName); + } + + /** The default {@link ExceptionProvider} instance. */ + ExceptionProvider DEFAULT = new ExceptionProvider() {}; + +} \ No newline at end of file diff --git a/samples/client/petstore/java/restclient-useSingleRequestParameter/.openapi-generator/FILES b/samples/client/petstore/java/restclient-useSingleRequestParameter/.openapi-generator/FILES index 555600856098..d7a6d76bf38f 100644 --- a/samples/client/petstore/java/restclient-useSingleRequestParameter/.openapi-generator/FILES +++ b/samples/client/petstore/java/restclient-useSingleRequestParameter/.openapi-generator/FILES @@ -72,6 +72,7 @@ pom.xml settings.gradle src/main/AndroidManifest.xml src/main/java/org/openapitools/client/ApiClient.java +src/main/java/org/openapitools/client/ExceptionProvider.java src/main/java/org/openapitools/client/JavaTimeFormatter.java src/main/java/org/openapitools/client/RFC3339DateFormat.java src/main/java/org/openapitools/client/RFC3339InstantDeserializer.java diff --git a/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java index f773ee249976..ba9fc446b26a 100644 --- a/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java @@ -141,6 +141,13 @@ protected String collectionToString(Collection collection) { protected final ObjectMapper mapper; protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this(null); @@ -289,6 +296,25 @@ public Map getAuthentications() { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -310,7 +336,7 @@ public void setBearerToken(String bearerToken) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -325,7 +351,7 @@ public void setBearerToken(Supplier tokenSupplier) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -339,7 +365,7 @@ public void setUsername(String username) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -353,7 +379,7 @@ public void setPassword(String password) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -367,7 +393,7 @@ public void setApiKey(String apiKey) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -381,7 +407,7 @@ public void setApiKeyPrefix(String apiKeyPrefix) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -405,7 +431,7 @@ public ApiClient setAccessToken(Supplier tokenSupplier) { return this; } } - throw new RuntimeException("No OAuth2 authentication configured!"); + throw exceptionProvider.oAuth2Exception(); } /** @@ -460,7 +486,7 @@ public Date parseDate(String str) { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -528,7 +554,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } @@ -537,7 +563,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { values.add(mapper.writeValueAsString(o)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -871,7 +897,7 @@ protected void updateParamsForAuth(String[] authNames, MultiValueMap + * The default implementation throws {@link RuntimeException}. To use custom exception + * types, implement this interface and pass the instance to + * {@link ApiClient#setExceptionProvider(ExceptionProvider)}. + *

+ *
{@code
+ * apiClient.setExceptionProvider(new ExceptionProvider() {
+ *     public RuntimeException bearerAuthException() {
+ *         return new MyAuthException("No Bearer authentication configured!");
+ *     }
+ * });
+ * }
+ */ +public interface ExceptionProvider { + + /** Returns an exception indicating that no Bearer authentication is configured. */ + default RuntimeException bearerAuthException() { + return new RuntimeException("No Bearer authentication configured!"); + } + + /** Returns an exception indicating that no HTTP basic authentication is configured. */ + default RuntimeException httpBasicAuthException() { + return new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Returns an exception indicating that no API key authentication is configured. */ + default RuntimeException apiKeyAuthException() { + return new RuntimeException("No API key authentication configured!"); + } + + /** Returns an exception indicating that no OAuth2 authentication is configured. */ + default RuntimeException oAuth2Exception() { + return new RuntimeException("No OAuth2 authentication configured!"); + } + + /** Wraps a date/time parse exception. */ + default RuntimeException dateTimeException(ParseException e) { + return new RuntimeException(e); + } + + /** Wraps a Jackson serialization/deserialization exception. */ + default RuntimeException jacksonException(Exception e) { + return new RuntimeException(e); + } + + /** + * Returns an exception indicating that the requested authentication scheme is not configured. + * + * @param authName the name of the authentication scheme that could not be found + * @return a {@link RestClientException} describing the missing authentication + */ + default RestClientException undefinedAuthenticationException(String authName) { + return new RestClientException("Authentication undefined: " + authName); + } + + /** The default {@link ExceptionProvider} instance. */ + ExceptionProvider DEFAULT = new ExceptionProvider() {}; + +} \ No newline at end of file diff --git a/samples/client/petstore/java/restclient/.openapi-generator/FILES b/samples/client/petstore/java/restclient/.openapi-generator/FILES index 555600856098..d7a6d76bf38f 100644 --- a/samples/client/petstore/java/restclient/.openapi-generator/FILES +++ b/samples/client/petstore/java/restclient/.openapi-generator/FILES @@ -72,6 +72,7 @@ pom.xml settings.gradle src/main/AndroidManifest.xml src/main/java/org/openapitools/client/ApiClient.java +src/main/java/org/openapitools/client/ExceptionProvider.java src/main/java/org/openapitools/client/JavaTimeFormatter.java src/main/java/org/openapitools/client/RFC3339DateFormat.java src/main/java/org/openapitools/client/RFC3339InstantDeserializer.java diff --git a/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/ApiClient.java index f773ee249976..ba9fc446b26a 100644 --- a/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/restclient/src/main/java/org/openapitools/client/ApiClient.java @@ -141,6 +141,13 @@ protected String collectionToString(Collection collection) { protected final ObjectMapper mapper; protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this(null); @@ -289,6 +296,25 @@ public Map getAuthentications() { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -310,7 +336,7 @@ public void setBearerToken(String bearerToken) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -325,7 +351,7 @@ public void setBearerToken(Supplier tokenSupplier) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -339,7 +365,7 @@ public void setUsername(String username) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -353,7 +379,7 @@ public void setPassword(String password) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -367,7 +393,7 @@ public void setApiKey(String apiKey) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -381,7 +407,7 @@ public void setApiKeyPrefix(String apiKeyPrefix) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -405,7 +431,7 @@ public ApiClient setAccessToken(Supplier tokenSupplier) { return this; } } - throw new RuntimeException("No OAuth2 authentication configured!"); + throw exceptionProvider.oAuth2Exception(); } /** @@ -460,7 +486,7 @@ public Date parseDate(String str) { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -528,7 +554,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } @@ -537,7 +563,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { values.add(mapper.writeValueAsString(o)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -871,7 +897,7 @@ protected void updateParamsForAuth(String[] authNames, MultiValueMap + * The default implementation throws {@link RuntimeException}. To use custom exception + * types, implement this interface and pass the instance to + * {@link ApiClient#setExceptionProvider(ExceptionProvider)}. + *

+ *
{@code
+ * apiClient.setExceptionProvider(new ExceptionProvider() {
+ *     public RuntimeException bearerAuthException() {
+ *         return new MyAuthException("No Bearer authentication configured!");
+ *     }
+ * });
+ * }
+ */ +public interface ExceptionProvider { + + /** Returns an exception indicating that no Bearer authentication is configured. */ + default RuntimeException bearerAuthException() { + return new RuntimeException("No Bearer authentication configured!"); + } + + /** Returns an exception indicating that no HTTP basic authentication is configured. */ + default RuntimeException httpBasicAuthException() { + return new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Returns an exception indicating that no API key authentication is configured. */ + default RuntimeException apiKeyAuthException() { + return new RuntimeException("No API key authentication configured!"); + } + + /** Returns an exception indicating that no OAuth2 authentication is configured. */ + default RuntimeException oAuth2Exception() { + return new RuntimeException("No OAuth2 authentication configured!"); + } + + /** Wraps a date/time parse exception. */ + default RuntimeException dateTimeException(ParseException e) { + return new RuntimeException(e); + } + + /** Wraps a Jackson serialization/deserialization exception. */ + default RuntimeException jacksonException(Exception e) { + return new RuntimeException(e); + } + + /** + * Returns an exception indicating that the requested authentication scheme is not configured. + * + * @param authName the name of the authentication scheme that could not be found + * @return a {@link RestClientException} describing the missing authentication + */ + default RestClientException undefinedAuthenticationException(String authName) { + return new RestClientException("Authentication undefined: " + authName); + } + + /** The default {@link ExceptionProvider} instance. */ + ExceptionProvider DEFAULT = new ExceptionProvider() {}; + +} \ No newline at end of file diff --git a/samples/client/petstore/java/webclient-jakarta/.openapi-generator/FILES b/samples/client/petstore/java/webclient-jakarta/.openapi-generator/FILES index 555600856098..d7a6d76bf38f 100644 --- a/samples/client/petstore/java/webclient-jakarta/.openapi-generator/FILES +++ b/samples/client/petstore/java/webclient-jakarta/.openapi-generator/FILES @@ -72,6 +72,7 @@ pom.xml settings.gradle src/main/AndroidManifest.xml src/main/java/org/openapitools/client/ApiClient.java +src/main/java/org/openapitools/client/ExceptionProvider.java src/main/java/org/openapitools/client/JavaTimeFormatter.java src/main/java/org/openapitools/client/RFC3339DateFormat.java src/main/java/org/openapitools/client/RFC3339InstantDeserializer.java diff --git a/samples/client/petstore/java/webclient-jakarta/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/webclient-jakarta/src/main/java/org/openapitools/client/ApiClient.java index 9490dcf43075..ed3bfb7bc562 100644 --- a/samples/client/petstore/java/webclient-jakarta/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/webclient-jakarta/src/main/java/org/openapitools/client/ApiClient.java @@ -110,6 +110,13 @@ protected String collectionToString(Collection collection) { protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this.dateFormat = createDefaultDateFormat(); @@ -236,6 +243,25 @@ public Map getAuthentications() { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -257,7 +283,7 @@ public void setBearerToken(String bearerToken) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -271,7 +297,7 @@ public void setUsername(String username) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -285,7 +311,7 @@ public void setPassword(String password) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -299,7 +325,7 @@ public void setApiKey(String apiKey) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -313,7 +339,7 @@ public void setApiKeyPrefix(String apiKeyPrefix) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -327,7 +353,7 @@ public void setAccessToken(String accessToken) { return; } } - throw new RuntimeException("No OAuth2 authentication configured!"); + throw exceptionProvider.oAuth2Exception(); } /** @@ -382,7 +408,7 @@ public Date parseDate(String str) { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -449,8 +475,8 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm } else { try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + throw exceptionProvider.jacksonException(e); } } @@ -459,7 +485,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { values.add(mapper.writeValueAsString(o)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -763,7 +789,7 @@ protected void updateParamsForAuth(String[] authNames, MultiValueMap + * The default implementation throws {@link RuntimeException}. To use custom exception + * types, implement this interface and pass the instance to + * {@link ApiClient#setExceptionProvider(ExceptionProvider)}. + *

+ *
{@code
+ * apiClient.setExceptionProvider(new ExceptionProvider() {
+ *     public RuntimeException bearerAuthException() {
+ *         return new MyAuthException("No Bearer authentication configured!");
+ *     }
+ * });
+ * }
+ */ +public interface ExceptionProvider { + + /** Returns an exception indicating that no Bearer authentication is configured. */ + default RuntimeException bearerAuthException() { + return new RuntimeException("No Bearer authentication configured!"); + } + + /** Returns an exception indicating that no HTTP basic authentication is configured. */ + default RuntimeException httpBasicAuthException() { + return new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Returns an exception indicating that no API key authentication is configured. */ + default RuntimeException apiKeyAuthException() { + return new RuntimeException("No API key authentication configured!"); + } + + /** Returns an exception indicating that no OAuth2 authentication is configured. */ + default RuntimeException oAuth2Exception() { + return new RuntimeException("No OAuth2 authentication configured!"); + } + + /** Wraps a date/time parse exception. */ + default RuntimeException dateTimeException(ParseException e) { + return new RuntimeException(e); + } + + /** Wraps a Jackson serialization/deserialization exception. */ + default RuntimeException jacksonException(Exception e) { + return new RuntimeException(e); + } + + /** + * Returns an exception indicating that the requested authentication scheme is not configured. + * + * @param authName the name of the authentication scheme that could not be found + * @return a {@link RestClientException} describing the missing authentication + */ + default RestClientException undefinedAuthenticationException(String authName) { + return new RestClientException("Authentication undefined: " + authName); + } + + /** The default {@link ExceptionProvider} instance. */ + ExceptionProvider DEFAULT = new ExceptionProvider() {}; + +} \ No newline at end of file diff --git a/samples/client/petstore/java/webclient-nullable-arrays/.openapi-generator/FILES b/samples/client/petstore/java/webclient-nullable-arrays/.openapi-generator/FILES index eedafa945afd..1466503e3b79 100644 --- a/samples/client/petstore/java/webclient-nullable-arrays/.openapi-generator/FILES +++ b/samples/client/petstore/java/webclient-nullable-arrays/.openapi-generator/FILES @@ -17,6 +17,7 @@ pom.xml settings.gradle src/main/AndroidManifest.xml src/main/java/org/openapitools/client/ApiClient.java +src/main/java/org/openapitools/client/ExceptionProvider.java src/main/java/org/openapitools/client/JavaTimeFormatter.java src/main/java/org/openapitools/client/RFC3339DateFormat.java src/main/java/org/openapitools/client/RFC3339InstantDeserializer.java diff --git a/samples/client/petstore/java/webclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/webclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java index 41bcc01749e7..2e3977a90497 100644 --- a/samples/client/petstore/java/webclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/webclient-nullable-arrays/src/main/java/org/openapitools/client/ApiClient.java @@ -109,6 +109,13 @@ protected String collectionToString(Collection collection) { protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this.dateFormat = createDefaultDateFormat(); @@ -230,6 +237,25 @@ public Map getAuthentications() { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -251,7 +277,7 @@ public void setBearerToken(String bearerToken) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -265,7 +291,7 @@ public void setUsername(String username) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -279,7 +305,7 @@ public void setPassword(String password) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -293,7 +319,7 @@ public void setApiKey(String apiKey) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -307,7 +333,7 @@ public void setApiKeyPrefix(String apiKeyPrefix) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -362,7 +388,7 @@ public Date parseDate(String str) { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -429,8 +455,8 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm } else { try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + throw exceptionProvider.jacksonException(e); } } @@ -439,7 +465,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { values.add(mapper.writeValueAsString(o)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -743,7 +769,7 @@ protected void updateParamsForAuth(String[] authNames, MultiValueMap + * The default implementation throws {@link RuntimeException}. To use custom exception + * types, implement this interface and pass the instance to + * {@link ApiClient#setExceptionProvider(ExceptionProvider)}. + *

+ *
{@code
+ * apiClient.setExceptionProvider(new ExceptionProvider() {
+ *     public RuntimeException bearerAuthException() {
+ *         return new MyAuthException("No Bearer authentication configured!");
+ *     }
+ * });
+ * }
+ */ +public interface ExceptionProvider { + + /** Returns an exception indicating that no Bearer authentication is configured. */ + default RuntimeException bearerAuthException() { + return new RuntimeException("No Bearer authentication configured!"); + } + + /** Returns an exception indicating that no HTTP basic authentication is configured. */ + default RuntimeException httpBasicAuthException() { + return new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Returns an exception indicating that no API key authentication is configured. */ + default RuntimeException apiKeyAuthException() { + return new RuntimeException("No API key authentication configured!"); + } + + /** Returns an exception indicating that no OAuth2 authentication is configured. */ + default RuntimeException oAuth2Exception() { + return new RuntimeException("No OAuth2 authentication configured!"); + } + + /** Wraps a date/time parse exception. */ + default RuntimeException dateTimeException(ParseException e) { + return new RuntimeException(e); + } + + /** Wraps a Jackson serialization/deserialization exception. */ + default RuntimeException jacksonException(Exception e) { + return new RuntimeException(e); + } + + /** + * Returns an exception indicating that the requested authentication scheme is not configured. + * + * @param authName the name of the authentication scheme that could not be found + * @return a {@link RestClientException} describing the missing authentication + */ + default RestClientException undefinedAuthenticationException(String authName) { + return new RestClientException("Authentication undefined: " + authName); + } + + /** The default {@link ExceptionProvider} instance. */ + ExceptionProvider DEFAULT = new ExceptionProvider() {}; + +} \ No newline at end of file diff --git a/samples/client/petstore/java/webclient-springBoot4-jackson3-jspecify/.openapi-generator/FILES b/samples/client/petstore/java/webclient-springBoot4-jackson3-jspecify/.openapi-generator/FILES index d59cb3fdbb35..fa0830b314a4 100644 --- a/samples/client/petstore/java/webclient-springBoot4-jackson3-jspecify/.openapi-generator/FILES +++ b/samples/client/petstore/java/webclient-springBoot4-jackson3-jspecify/.openapi-generator/FILES @@ -17,6 +17,7 @@ pom.xml settings.gradle src/main/AndroidManifest.xml src/main/java/org/openapitools/client/ApiClient.java +src/main/java/org/openapitools/client/ExceptionProvider.java src/main/java/org/openapitools/client/JavaTimeFormatter.java src/main/java/org/openapitools/client/RFC3339DateFormat.java src/main/java/org/openapitools/client/RFC3339InstantDeserializer.java diff --git a/samples/client/petstore/java/webclient-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/webclient-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ApiClient.java index de847d3ee896..b30e50883e2f 100644 --- a/samples/client/petstore/java/webclient-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/webclient-springBoot4-jackson3-jspecify/src/main/java/org/openapitools/client/ApiClient.java @@ -107,6 +107,13 @@ protected String collectionToString(Collection collection) { protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this.dateFormat = createDefaultDateFormat(); @@ -222,6 +229,25 @@ public Map getAuthentications() { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -243,7 +269,7 @@ public void setBearerToken(String bearerToken) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -257,7 +283,7 @@ public void setUsername(String username) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -271,7 +297,7 @@ public void setPassword(String password) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -285,7 +311,7 @@ public void setApiKey(String apiKey) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -299,7 +325,7 @@ public void setApiKeyPrefix(String apiKeyPrefix) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -354,7 +380,7 @@ public Date parseDate(String str) { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -421,8 +447,8 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm } else { try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); - } catch (JacksonException e) { - throw new RuntimeException(e); + } catch (JacksonException e) { + throw exceptionProvider.jacksonException(e); } } @@ -431,7 +457,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { values.add(mapper.writeValueAsString(o)); } catch (JacksonException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -735,7 +761,7 @@ protected void updateParamsForAuth(String[] authNames, MultiValueMap + * The default implementation throws {@link RuntimeException}. To use custom exception + * types, implement this interface and pass the instance to + * {@link ApiClient#setExceptionProvider(ExceptionProvider)}. + *

+ *
{@code
+ * apiClient.setExceptionProvider(new ExceptionProvider() {
+ *     public RuntimeException bearerAuthException() {
+ *         return new MyAuthException("No Bearer authentication configured!");
+ *     }
+ * });
+ * }
+ */ +public interface ExceptionProvider { + + /** Returns an exception indicating that no Bearer authentication is configured. */ + default RuntimeException bearerAuthException() { + return new RuntimeException("No Bearer authentication configured!"); + } + + /** Returns an exception indicating that no HTTP basic authentication is configured. */ + default RuntimeException httpBasicAuthException() { + return new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Returns an exception indicating that no API key authentication is configured. */ + default RuntimeException apiKeyAuthException() { + return new RuntimeException("No API key authentication configured!"); + } + + /** Returns an exception indicating that no OAuth2 authentication is configured. */ + default RuntimeException oAuth2Exception() { + return new RuntimeException("No OAuth2 authentication configured!"); + } + + /** Wraps a date/time parse exception. */ + default RuntimeException dateTimeException(ParseException e) { + return new RuntimeException(e); + } + + /** Wraps a Jackson serialization/deserialization exception. */ + default RuntimeException jacksonException(Exception e) { + return new RuntimeException(e); + } + + /** + * Returns an exception indicating that the requested authentication scheme is not configured. + * + * @param authName the name of the authentication scheme that could not be found + * @return a {@link RestClientException} describing the missing authentication + */ + default RestClientException undefinedAuthenticationException(String authName) { + return new RestClientException("Authentication undefined: " + authName); + } + + /** The default {@link ExceptionProvider} instance. */ + ExceptionProvider DEFAULT = new ExceptionProvider() {}; + +} \ No newline at end of file diff --git a/samples/client/petstore/java/webclient-springBoot4-jackson3/.openapi-generator/FILES b/samples/client/petstore/java/webclient-springBoot4-jackson3/.openapi-generator/FILES index 345da529f8a1..94cc6f4676ac 100644 --- a/samples/client/petstore/java/webclient-springBoot4-jackson3/.openapi-generator/FILES +++ b/samples/client/petstore/java/webclient-springBoot4-jackson3/.openapi-generator/FILES @@ -19,6 +19,7 @@ pom.xml settings.gradle src/main/AndroidManifest.xml src/main/java/org/openapitools/client/ApiClient.java +src/main/java/org/openapitools/client/ExceptionProvider.java src/main/java/org/openapitools/client/JavaTimeFormatter.java src/main/java/org/openapitools/client/RFC3339DateFormat.java src/main/java/org/openapitools/client/RFC3339InstantDeserializer.java diff --git a/samples/client/petstore/java/webclient-springBoot4-jackson3/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/webclient-springBoot4-jackson3/src/main/java/org/openapitools/client/ApiClient.java index 2d4f52eae16b..dbda70badc9d 100644 --- a/samples/client/petstore/java/webclient-springBoot4-jackson3/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/webclient-springBoot4-jackson3/src/main/java/org/openapitools/client/ApiClient.java @@ -108,6 +108,13 @@ protected String collectionToString(Collection collection) { protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this.dateFormat = createDefaultDateFormat(); @@ -224,6 +231,25 @@ public Map getAuthentications() { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -245,7 +271,7 @@ public void setBearerToken(String bearerToken) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -259,7 +285,7 @@ public void setUsername(String username) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -273,7 +299,7 @@ public void setPassword(String password) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -287,7 +313,7 @@ public void setApiKey(String apiKey) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -301,7 +327,7 @@ public void setApiKeyPrefix(String apiKeyPrefix) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -315,7 +341,7 @@ public void setAccessToken(String accessToken) { return; } } - throw new RuntimeException("No OAuth2 authentication configured!"); + throw exceptionProvider.oAuth2Exception(); } /** @@ -370,7 +396,7 @@ public Date parseDate(String str) { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -437,8 +463,8 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm } else { try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); - } catch (JacksonException e) { - throw new RuntimeException(e); + } catch (JacksonException e) { + throw exceptionProvider.jacksonException(e); } } @@ -447,7 +473,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { values.add(mapper.writeValueAsString(o)); } catch (JacksonException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -751,7 +777,7 @@ protected void updateParamsForAuth(String[] authNames, MultiValueMap + * The default implementation throws {@link RuntimeException}. To use custom exception + * types, implement this interface and pass the instance to + * {@link ApiClient#setExceptionProvider(ExceptionProvider)}. + *

+ *
{@code
+ * apiClient.setExceptionProvider(new ExceptionProvider() {
+ *     public RuntimeException bearerAuthException() {
+ *         return new MyAuthException("No Bearer authentication configured!");
+ *     }
+ * });
+ * }
+ */ +public interface ExceptionProvider { + + /** Returns an exception indicating that no Bearer authentication is configured. */ + default RuntimeException bearerAuthException() { + return new RuntimeException("No Bearer authentication configured!"); + } + + /** Returns an exception indicating that no HTTP basic authentication is configured. */ + default RuntimeException httpBasicAuthException() { + return new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Returns an exception indicating that no API key authentication is configured. */ + default RuntimeException apiKeyAuthException() { + return new RuntimeException("No API key authentication configured!"); + } + + /** Returns an exception indicating that no OAuth2 authentication is configured. */ + default RuntimeException oAuth2Exception() { + return new RuntimeException("No OAuth2 authentication configured!"); + } + + /** Wraps a date/time parse exception. */ + default RuntimeException dateTimeException(ParseException e) { + return new RuntimeException(e); + } + + /** Wraps a Jackson serialization/deserialization exception. */ + default RuntimeException jacksonException(Exception e) { + return new RuntimeException(e); + } + + /** + * Returns an exception indicating that the requested authentication scheme is not configured. + * + * @param authName the name of the authentication scheme that could not be found + * @return a {@link RestClientException} describing the missing authentication + */ + default RestClientException undefinedAuthenticationException(String authName) { + return new RestClientException("Authentication undefined: " + authName); + } + + /** The default {@link ExceptionProvider} instance. */ + ExceptionProvider DEFAULT = new ExceptionProvider() {}; + +} \ No newline at end of file diff --git a/samples/client/petstore/java/webclient-swagger2/.openapi-generator/FILES b/samples/client/petstore/java/webclient-swagger2/.openapi-generator/FILES index 555600856098..d7a6d76bf38f 100644 --- a/samples/client/petstore/java/webclient-swagger2/.openapi-generator/FILES +++ b/samples/client/petstore/java/webclient-swagger2/.openapi-generator/FILES @@ -72,6 +72,7 @@ pom.xml settings.gradle src/main/AndroidManifest.xml src/main/java/org/openapitools/client/ApiClient.java +src/main/java/org/openapitools/client/ExceptionProvider.java src/main/java/org/openapitools/client/JavaTimeFormatter.java src/main/java/org/openapitools/client/RFC3339DateFormat.java src/main/java/org/openapitools/client/RFC3339InstantDeserializer.java diff --git a/samples/client/petstore/java/webclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/webclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java index f7f65e1c23d4..d81de1a1b88d 100644 --- a/samples/client/petstore/java/webclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/webclient-swagger2/src/main/java/org/openapitools/client/ApiClient.java @@ -110,6 +110,13 @@ protected String collectionToString(Collection collection) { protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this.dateFormat = createDefaultDateFormat(); @@ -236,6 +243,25 @@ public Map getAuthentications() { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -257,7 +283,7 @@ public void setBearerToken(String bearerToken) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -271,7 +297,7 @@ public void setUsername(String username) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -285,7 +311,7 @@ public void setPassword(String password) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -299,7 +325,7 @@ public void setApiKey(String apiKey) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -313,7 +339,7 @@ public void setApiKeyPrefix(String apiKeyPrefix) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -327,7 +353,7 @@ public void setAccessToken(String accessToken) { return; } } - throw new RuntimeException("No OAuth2 authentication configured!"); + throw exceptionProvider.oAuth2Exception(); } /** @@ -382,7 +408,7 @@ public Date parseDate(String str) { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -449,8 +475,8 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm } else { try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + throw exceptionProvider.jacksonException(e); } } @@ -459,7 +485,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { values.add(mapper.writeValueAsString(o)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -763,7 +789,7 @@ protected void updateParamsForAuth(String[] authNames, MultiValueMap + * The default implementation throws {@link RuntimeException}. To use custom exception + * types, implement this interface and pass the instance to + * {@link ApiClient#setExceptionProvider(ExceptionProvider)}. + *

+ *
{@code
+ * apiClient.setExceptionProvider(new ExceptionProvider() {
+ *     public RuntimeException bearerAuthException() {
+ *         return new MyAuthException("No Bearer authentication configured!");
+ *     }
+ * });
+ * }
+ */ +public interface ExceptionProvider { + + /** Returns an exception indicating that no Bearer authentication is configured. */ + default RuntimeException bearerAuthException() { + return new RuntimeException("No Bearer authentication configured!"); + } + + /** Returns an exception indicating that no HTTP basic authentication is configured. */ + default RuntimeException httpBasicAuthException() { + return new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Returns an exception indicating that no API key authentication is configured. */ + default RuntimeException apiKeyAuthException() { + return new RuntimeException("No API key authentication configured!"); + } + + /** Returns an exception indicating that no OAuth2 authentication is configured. */ + default RuntimeException oAuth2Exception() { + return new RuntimeException("No OAuth2 authentication configured!"); + } + + /** Wraps a date/time parse exception. */ + default RuntimeException dateTimeException(ParseException e) { + return new RuntimeException(e); + } + + /** Wraps a Jackson serialization/deserialization exception. */ + default RuntimeException jacksonException(Exception e) { + return new RuntimeException(e); + } + + /** + * Returns an exception indicating that the requested authentication scheme is not configured. + * + * @param authName the name of the authentication scheme that could not be found + * @return a {@link RestClientException} describing the missing authentication + */ + default RestClientException undefinedAuthenticationException(String authName) { + return new RestClientException("Authentication undefined: " + authName); + } + + /** The default {@link ExceptionProvider} instance. */ + ExceptionProvider DEFAULT = new ExceptionProvider() {}; + +} \ No newline at end of file diff --git a/samples/client/petstore/java/webclient-useSingleRequestParameter/.openapi-generator/FILES b/samples/client/petstore/java/webclient-useSingleRequestParameter/.openapi-generator/FILES index 555600856098..d7a6d76bf38f 100644 --- a/samples/client/petstore/java/webclient-useSingleRequestParameter/.openapi-generator/FILES +++ b/samples/client/petstore/java/webclient-useSingleRequestParameter/.openapi-generator/FILES @@ -72,6 +72,7 @@ pom.xml settings.gradle src/main/AndroidManifest.xml src/main/java/org/openapitools/client/ApiClient.java +src/main/java/org/openapitools/client/ExceptionProvider.java src/main/java/org/openapitools/client/JavaTimeFormatter.java src/main/java/org/openapitools/client/RFC3339DateFormat.java src/main/java/org/openapitools/client/RFC3339InstantDeserializer.java diff --git a/samples/client/petstore/java/webclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/webclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java index f7f65e1c23d4..d81de1a1b88d 100644 --- a/samples/client/petstore/java/webclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/webclient-useSingleRequestParameter/src/main/java/org/openapitools/client/ApiClient.java @@ -110,6 +110,13 @@ protected String collectionToString(Collection collection) { protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this.dateFormat = createDefaultDateFormat(); @@ -236,6 +243,25 @@ public Map getAuthentications() { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -257,7 +283,7 @@ public void setBearerToken(String bearerToken) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -271,7 +297,7 @@ public void setUsername(String username) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -285,7 +311,7 @@ public void setPassword(String password) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -299,7 +325,7 @@ public void setApiKey(String apiKey) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -313,7 +339,7 @@ public void setApiKeyPrefix(String apiKeyPrefix) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -327,7 +353,7 @@ public void setAccessToken(String accessToken) { return; } } - throw new RuntimeException("No OAuth2 authentication configured!"); + throw exceptionProvider.oAuth2Exception(); } /** @@ -382,7 +408,7 @@ public Date parseDate(String str) { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -449,8 +475,8 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm } else { try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + throw exceptionProvider.jacksonException(e); } } @@ -459,7 +485,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { values.add(mapper.writeValueAsString(o)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -763,7 +789,7 @@ protected void updateParamsForAuth(String[] authNames, MultiValueMap + * The default implementation throws {@link RuntimeException}. To use custom exception + * types, implement this interface and pass the instance to + * {@link ApiClient#setExceptionProvider(ExceptionProvider)}. + *

+ *
{@code
+ * apiClient.setExceptionProvider(new ExceptionProvider() {
+ *     public RuntimeException bearerAuthException() {
+ *         return new MyAuthException("No Bearer authentication configured!");
+ *     }
+ * });
+ * }
+ */ +public interface ExceptionProvider { + + /** Returns an exception indicating that no Bearer authentication is configured. */ + default RuntimeException bearerAuthException() { + return new RuntimeException("No Bearer authentication configured!"); + } + + /** Returns an exception indicating that no HTTP basic authentication is configured. */ + default RuntimeException httpBasicAuthException() { + return new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Returns an exception indicating that no API key authentication is configured. */ + default RuntimeException apiKeyAuthException() { + return new RuntimeException("No API key authentication configured!"); + } + + /** Returns an exception indicating that no OAuth2 authentication is configured. */ + default RuntimeException oAuth2Exception() { + return new RuntimeException("No OAuth2 authentication configured!"); + } + + /** Wraps a date/time parse exception. */ + default RuntimeException dateTimeException(ParseException e) { + return new RuntimeException(e); + } + + /** Wraps a Jackson serialization/deserialization exception. */ + default RuntimeException jacksonException(Exception e) { + return new RuntimeException(e); + } + + /** + * Returns an exception indicating that the requested authentication scheme is not configured. + * + * @param authName the name of the authentication scheme that could not be found + * @return a {@link RestClientException} describing the missing authentication + */ + default RestClientException undefinedAuthenticationException(String authName) { + return new RestClientException("Authentication undefined: " + authName); + } + + /** The default {@link ExceptionProvider} instance. */ + ExceptionProvider DEFAULT = new ExceptionProvider() {}; + +} \ No newline at end of file diff --git a/samples/client/petstore/java/webclient/.openapi-generator/FILES b/samples/client/petstore/java/webclient/.openapi-generator/FILES index 555600856098..d7a6d76bf38f 100644 --- a/samples/client/petstore/java/webclient/.openapi-generator/FILES +++ b/samples/client/petstore/java/webclient/.openapi-generator/FILES @@ -72,6 +72,7 @@ pom.xml settings.gradle src/main/AndroidManifest.xml src/main/java/org/openapitools/client/ApiClient.java +src/main/java/org/openapitools/client/ExceptionProvider.java src/main/java/org/openapitools/client/JavaTimeFormatter.java src/main/java/org/openapitools/client/RFC3339DateFormat.java src/main/java/org/openapitools/client/RFC3339InstantDeserializer.java diff --git a/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java index f7f65e1c23d4..d81de1a1b88d 100644 --- a/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/webclient/src/main/java/org/openapitools/client/ApiClient.java @@ -110,6 +110,13 @@ protected String collectionToString(Collection collection) { protected Map authentications; + /** + * The {@link ExceptionProvider} used to create exceptions thrown by this client. + * Defaults to {@link ExceptionProvider#DEFAULT}. Can be replaced to customize the exceptions + * thrown by this client by calling {@link #setExceptionProvider(ExceptionProvider)}. + */ + protected ExceptionProvider exceptionProvider = ExceptionProvider.DEFAULT; + public ApiClient() { this.dateFormat = createDefaultDateFormat(); @@ -236,6 +243,25 @@ public Map getAuthentications() { return authentications; } + /** + * Get the current {@link ExceptionProvider}. + * @return the exception provider + */ + public ExceptionProvider getExceptionProvider() { + return exceptionProvider; + } + + /** + * Set a custom {@link ExceptionProvider} to control which exception types are thrown + * by this client. + * @param exceptionProvider the exception provider + * @return this client instance + */ + public ApiClient setExceptionProvider(ExceptionProvider exceptionProvider) { + this.exceptionProvider = exceptionProvider; + return this; + } + /** * Get authentication for the given name. * @@ -257,7 +283,7 @@ public void setBearerToken(String bearerToken) { return; } } - throw new RuntimeException("No Bearer authentication configured!"); + throw exceptionProvider.bearerAuthException(); } /** @@ -271,7 +297,7 @@ public void setUsername(String username) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -285,7 +311,7 @@ public void setPassword(String password) { return; } } - throw new RuntimeException("No HTTP basic authentication configured!"); + throw exceptionProvider.httpBasicAuthException(); } /** @@ -299,7 +325,7 @@ public void setApiKey(String apiKey) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -313,7 +339,7 @@ public void setApiKeyPrefix(String apiKeyPrefix) { return; } } - throw new RuntimeException("No API key authentication configured!"); + throw exceptionProvider.apiKeyAuthException(); } /** @@ -327,7 +353,7 @@ public void setAccessToken(String accessToken) { return; } } - throw new RuntimeException("No OAuth2 authentication configured!"); + throw exceptionProvider.oAuth2Exception(); } /** @@ -382,7 +408,7 @@ public Date parseDate(String str) { try { return dateFormat.parse(str); } catch (ParseException e) { - throw new RuntimeException(e); + throw exceptionProvider.dateTimeException(e); } } @@ -449,8 +475,8 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm } else { try { return parameterToMultiValueMap(collectionFormat, name, mapper.writeValueAsString(value)); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); + } catch (JsonProcessingException e) { + throw exceptionProvider.jacksonException(e); } } @@ -459,7 +485,7 @@ public MultiValueMap parameterToMultiValueMapJson(CollectionForm try { values.add(mapper.writeValueAsString(o)); } catch (JsonProcessingException e) { - throw new RuntimeException(e); + throw exceptionProvider.jacksonException(e); } } return parameterToMultiValueMap(collectionFormat, name, "[" + StringUtils.collectionToDelimitedString(values, collectionFormat.separator) + "]"); @@ -763,7 +789,7 @@ protected void updateParamsForAuth(String[] authNames, MultiValueMap + * The default implementation throws {@link RuntimeException}. To use custom exception + * types, implement this interface and pass the instance to + * {@link ApiClient#setExceptionProvider(ExceptionProvider)}. + *

+ *
{@code
+ * apiClient.setExceptionProvider(new ExceptionProvider() {
+ *     public RuntimeException bearerAuthException() {
+ *         return new MyAuthException("No Bearer authentication configured!");
+ *     }
+ * });
+ * }
+ */ +public interface ExceptionProvider { + + /** Returns an exception indicating that no Bearer authentication is configured. */ + default RuntimeException bearerAuthException() { + return new RuntimeException("No Bearer authentication configured!"); + } + + /** Returns an exception indicating that no HTTP basic authentication is configured. */ + default RuntimeException httpBasicAuthException() { + return new RuntimeException("No HTTP basic authentication configured!"); + } + + /** Returns an exception indicating that no API key authentication is configured. */ + default RuntimeException apiKeyAuthException() { + return new RuntimeException("No API key authentication configured!"); + } + + /** Returns an exception indicating that no OAuth2 authentication is configured. */ + default RuntimeException oAuth2Exception() { + return new RuntimeException("No OAuth2 authentication configured!"); + } + + /** Wraps a date/time parse exception. */ + default RuntimeException dateTimeException(ParseException e) { + return new RuntimeException(e); + } + + /** Wraps a Jackson serialization/deserialization exception. */ + default RuntimeException jacksonException(Exception e) { + return new RuntimeException(e); + } + + /** + * Returns an exception indicating that the requested authentication scheme is not configured. + * + * @param authName the name of the authentication scheme that could not be found + * @return a {@link RestClientException} describing the missing authentication + */ + default RestClientException undefinedAuthenticationException(String authName) { + return new RestClientException("Authentication undefined: " + authName); + } + + /** The default {@link ExceptionProvider} instance. */ + ExceptionProvider DEFAULT = new ExceptionProvider() {}; + +} \ No newline at end of file