jenkins-rest icon indicating copy to clipboard operation
jenkins-rest copied to clipboard

Better processing of JSON error responses in Http Response body

Open martinda opened this issue 3 years ago • 0 comments

I would like better processing of JSON error responses. Currently when the error response is in the body of the Http response, it is not processed as json, it is stored as plain text in the exception message. This means that the user code has to parse the exception error message in order to extract the real error message. It would be easier if the json body was processed so users would not have to do it.

Here is an example. The request:

org.jclouds.http.internal.JavaUrlHttpCommandExecutorService - Sending request -1829602346: POST http://127.0.0.1:8080/user/admin/descriptorByName/jenkins.security.ApiTokenProperty/revoke HTTP/1.1
jclouds.wire - >> "tokenUuid="
jclouds.headers - >> POST http://127.0.0.1:8080/user/admin/descriptorByName/jenkins.security.ApiTokenProperty/revoke HTTP/1.1
jclouds.headers - >> Accept: application/json
jclouds.headers - >> Authorization: Basic YWRtaW46YWRtaW4=
jclouds.headers - >> Jenkins-Crumb: ea6c09c3fcf0a7638d58096d11fbc608fa8f05dcf02b3072bd75ef38a9e61f36
jclouds.headers - >> Cookie: JSESSIONID.f870fd5d=node08g9exl9u4f7516eqezzki3jhz8.node0; Path=/; HttpOnly
jclouds.headers - >> Content-Type: application/x-www-form-urlencoded
jclouds.headers - >> Content-Length: 10

The response:

org.jclouds.http.internal.JavaUrlHttpCommandExecutorService - Receiving response -1829602346: HTTP/1.1 400 Bad Request
jclouds.headers - << HTTP/1.1 400 Bad Request
jclouds.headers - << Server: Jetty(9.4.39.v20210325)
jclouds.headers - << X-Content-Type-Options: nosniff
jclouds.headers - << Cache-Control: must-revalidate,no-cache,no-store
jclouds.headers - << Content-Type: application/json
jclouds.headers - << Content-Length: 161
jclouds.wire - << "{[\n]"
jclouds.wire - << ""servlet":"Stapler",[\n]"
jclouds.wire - << ""message":"The tokenUuid cannot be empty",[\n]"
jclouds.wire - << ""url":"/user/admin/descriptorByName/jenkins.security.ApiTokenProperty/revoke",[\n]"
jclouds.wire - << ""status":"400"[\n]"
jclouds.wire - << "}"

Currently, this is handled in JenkinsErrorHandler.java. The whole message is stored in the ErrorsHolder of RequestStatus as a plain text message. It looks like this:

    RequestStatus{value=false, errors=[Error{context=null, message={
    "servlet":"Stapler",
    "message":"The tokenUuid cannot be empty",
    "url":"/user/admin/descriptorByName/jenkins.security.ApiTokenProperty/revoke",
    "status":"400"
    }, exceptionName=java.lang.IllegalArgumentException}]}

According to this conversation, the json would be the same for all Jetty based installations.

martinda avatar Jan 04 '22 18:01 martinda