add option, update api doc with better error handling

This commit is contained in:
WILLIAM CHENG 2021-12-02 10:10:27 +08:00
parent 8d8c86728a
commit bfb45f4d67
2 changed files with 28 additions and 5 deletions

View File

@ -67,6 +67,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
public static final String SUPPORT_STREAMING = "supportStreaming";
public static final String GRADLE_PROPERTIES= "gradleProperties";
public static final String ERROR_OBJECT_TYPE= "errorObjectType";
public static final String ERROR_OBJECT_SUBTYPE= "errorObjectSubtype";
public static final String PLAY_24 = "play24";
public static final String PLAY_25 = "play25";
@ -120,6 +121,7 @@ public class JavaClientCodegen extends AbstractJavaCodegen
protected boolean supportStreaming = false;
protected String gradleProperties;
protected String errorObjectType;
protected List<String> errorObjectSubtype;
protected String authFolder;
protected String serializationLibrary = null;
@ -351,6 +353,11 @@ public class JavaClientCodegen extends AbstractJavaCodegen
}
additionalProperties.put(ERROR_OBJECT_TYPE, errorObjectType);
if (additionalProperties.containsKey(ERROR_OBJECT_SUBTYPE)) {
this.setErrorObjectSubtype((List<String>)additionalProperties.get(ERROR_OBJECT_SUBTYPE));
}
additionalProperties.put(ERROR_OBJECT_SUBTYPE, errorObjectSubtype);
final String invokerFolder = (sourceFolder + '/' + invokerPackage).replace(".", "/");
final String apiFolder = (sourceFolder + '/' + apiPackage).replace(".", "/");
final String modelsFolder = (sourceFolder + File.separator + modelPackage().replace('.', File.separatorChar)).replace('/', File.separatorChar);
@ -1042,6 +1049,10 @@ public class JavaClientCodegen extends AbstractJavaCodegen
this.errorObjectType= errorObjectType;
}
public void setErrorObjectSubtype(final List<String> errorObjectSubtype) {
this.errorObjectSubtype= errorObjectSubtype;
}
/**
* Serialization library.
*

View File

@ -63,11 +63,23 @@ public class Example {
.execute();{{/vendorExtensions.x-group-parameters}}{{#returnType}}
System.out.println(result);{{/returnType}}
} catch (ApiException e) {
System.err.println("Exception when calling {{{classname}}}#{{{operationId}}}");
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Response headers: " + e.getResponseHeaders());
e.printStackTrace();
{{=< >=}}
<#errorObjectSubtype>
<^-first>} else </-first>if (e.getErrorObject() instanceof <&.>) {
// do something here
<#-last>
} else {
// something else happened
System.err.println("Exception when calling {{{classname}}}#{{{operationId}}}");
System.err.println("Status code: " + e.getCode());
System.err.println("Reason: " + e.getResponseBody());
System.err.println("Response headers: " + e.getResponseHeaders());
e.printStackTrace();
}
</-last>
</errorObjectSubtype>
<={{ }}=>
}
}
}