Skip to content

Commit 0da9762

Browse files
authored
Fix NPE when getting error message and add HTTP response information (#4)
* Fix NPE when getting error message and add HTTP response information Signed-off-by: jorgee <[email protected]> * Print entity response when it is not an Error Message Signed-off-by: jorgee <[email protected]> * add custom exception and remove message parsing Signed-off-by: jorgee <[email protected]> --------- Signed-off-by: jorgee <[email protected]>
1 parent 639661e commit 0da9762

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/main/groovy/io/nextflow/gradle/registry/RegistryClient.groovy

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ package io.nextflow.gradle.registry
22

33
import com.google.gson.Gson
44
import groovy.transform.CompileStatic
5+
import groovy.util.logging.Slf4j
6+
import org.apache.http.client.methods.CloseableHttpResponse
57
import org.apache.http.client.methods.HttpPost
68
import org.apache.http.entity.mime.MultipartEntityBuilder
79
import org.apache.http.impl.client.HttpClients
10+
import org.apache.http.util.EntityUtils
811

12+
@Slf4j
913
@CompileStatic
1014
class RegistryClient {
1115
private final Gson gson = new Gson()
@@ -33,18 +37,21 @@ class RegistryClient {
3337
def rep = http.execute(req)) {
3438

3539
if (rep.statusLine.statusCode != 200) {
36-
def err = gson.fromJson(new InputStreamReader(rep.entity.content), ErrorResponse)
37-
throw new RuntimeException("Failed to publish plugin: ${err.type} - ${err.message}")
40+
throw new RegistryPublishException(getErrorMessage(rep))
3841
}
3942
} catch (ConnectException e) {
4043
throw new RuntimeException("Unable to connect to plugin repository: (${e.message})")
4144
}
4245
}
4346

44-
// ----------------------------------------------------------------------------
45-
46-
private static class ErrorResponse {
47-
String type
48-
String message
47+
private String getErrorMessage(CloseableHttpResponse rep) {
48+
def message = "Failed to publish plugin to registry $url: HTTP Response:${rep.statusLine}"
49+
if( rep.entity ) {
50+
final String entityStr = EntityUtils.toString(rep.entity)
51+
if (entityStr) {
52+
return "$message - $entityStr".toString()
53+
}
54+
}
55+
return message.toString()
4956
}
5057
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.nextflow.gradle.registry
2+
3+
/**
4+
* Custom exception class for registry publish task
5+
*/
6+
class RegistryPublishException extends Exception{
7+
RegistryPublishException(String s) {
8+
super(s)
9+
}
10+
}

0 commit comments

Comments
 (0)