artifactory-client-java
artifactory-client-java copied to clipboard
Improve 40x error handler to give feedback in comment exception
Hi,
When you are using the client (here v2.3.2), some error could occurs, like permissions (bad login/password, user not authorized to write in repo/path, ...).
In this case, with this snippet:
File file = new File("target", "test.file");
FileUtils.write(file, "Hello world", Charset.defaultCharset());
Artifactory artifactory = ArtifactoryClient.create("http://artifactory.company.com", "foo", "bar");
artifactory.repository("any-repository").upload("subPathNoPermission/test.file", file).doUpload();
artifactory.close();
When the login/password is bad, the exception is:
org.apache.http.client.ClientProtocolException
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:886)
[...]
at org.jfrog.artifactory.client.impl.UploadableArtifactImpl$_uploadContent_closure1.doCall(UploadableArtifactImpl.groovy:75)
[...]
at org.jfrog.artifactory.client.impl.UploadableArtifactImpl.uploadContent(UploadableArtifactImpl.groovy:74)
[...]
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
at org.jfrog.artifactory.client.impl.UploadableArtifactImpl.doUpload(UploadableArtifactImpl.groovy:53)
[...]
Caused by: org.apache.http.client.NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity.
at org.apache.http.impl.client.DefaultRequestDirector.tryExecute(DefaultRequestDirector.java:663)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:487)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:882)
... 81 more
When the login has not permissions on repo/path:
groovyx.net.http.HttpResponseException: Forbidden
at groovyx.net.http.RESTClient.defaultFailureHandler(RESTClient.java:263)
[...]
at org.jfrog.artifactory.client.impl.UploadableArtifactImpl.doUpload(UploadableArtifactImpl.groovy:53)
[...]
A useful improvement could be to write the 40x error code & JSon message in the Java exception.
Possible, because on bad login/password, DEBUG shows (with Artifactory v4.13.0):
org.apache.http.wire - << "{[\n]"
org.apache.http.wire - << " "errors" : [ {[\n]"
org.apache.http.wire - << " "status" : 401,[\n]"
org.apache.http.wire - << " "message" : "Bad credentials"[\n]"
org.apache.http.wire - << " } ][\n]"
org.apache.http.wire - << "}"
For permissions:
org.apache.http.wire - << "{[\n]"
org.apache.http.wire - << " "errors" : [ {[\n]"
org.apache.http.wire - << " "status" : 403,[\n]"
org.apache.http.wire - << " "message" : "User foo is not permitted to deploy or cache 'subPathNoPermission/test.file' into 'any-repository:subPathNoPermission/test.file'."[\n]"
org.apache.http.wire - << " } ][\n]"
org.apache.http.wire - << "}"