/*
* The MIT License
*
* Copyright 2014 CloudBees.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.cloudbees.mtslaves.client;
import net.sf.json.JSONObject;
import java.io.IOException;
import java.net.URL;
/**
* Indicates an exception from the server whose payload is JSON.
*
* @author Kohsuke Kawaguchi
*/
public class ServerException extends IOException {
private final JSONObject json;
private final int statusCode;
private final String statusMessage;
private final URL url;
public ServerException(String message, JSONObject json, int statusCode, String statusMessage, URL url) {
super(message);
this.json = json;
this.statusCode = statusCode;
this.statusMessage = statusMessage;
this.url = url;
}
public JSONObject getJson() {
return json;
}
public int getStatusCode() {
return statusCode;
}
public String getStatusMessage() {
return statusMessage;
}
public URL getUrl() {
return url;
}
@Override
public String toString() {
JSONObject _json = JSONObject.fromObject(json);
String cause = _json.optString("cause");
if (cause.isEmpty()) {
return super.toString() + "\n" + _json.toString(2);
} else {
_json.put("cause", "…");
return super.toString() + "\n" + _json.toString(2) + "\nCaused on server by: " + cause + "[end of server exception]";
}
}
}