package io.spacedog.client.http;

import java.util.Map;

import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.collect.Maps;

public class SpaceException extends RuntimeException {

	private static final long serialVersionUID = 491847884892423601L;

	private int httpStatus;
	private String code;
	private JsonNode details;
	private Map<String, String> headers = Maps.newLinkedHashMap();

	public SpaceException(int httpStatus, String message, Object... args) {
		this(fromStatusToCode(httpStatus), httpStatus, message, args);
	}

	public SpaceException(String code, int httpStatus, String message, Object... args) {
		super(String.format(message, args));
		this.httpStatus = httpStatus;
		this.code = code;
	}

	public SpaceException(int httpStatus, Throwable cause, String message, Object... args) {
		this(fromStatusToCode(httpStatus), httpStatus, cause, message, args);
	}

	public SpaceException(String code, int httpStatus, Throwable cause, String message, Object... args) {
		super(String.format(message, args), cause);
		this.httpStatus = httpStatus;
		this.code = code;
	}

	public int httpStatus() {
		return httpStatus;
	}

	public String code() {
		return code;
	}

	public SpaceException code(String code) {
		this.code = code;
		return this;
	}

	public JsonNode details() {
		return details;
	}

	public SpaceException details(JsonNode details) {
		this.details = details;
		return this;
	}

	public SpaceException cause(Throwable t) {
		this.initCause(t);
		return this;
	}

	public SpaceException withHeader(String key, String value) {
		headers.put(key, value);
		return this;
	}

	public Map<String, String> headers() {
		return headers;
	}

	//
	// implementation
	//

	private static String fromStatusToCode(int httpStatus) {
		return Integer.toString(httpStatus);
	}

}

@@ -138,16 +138,49 @@
 .Maps;%0A%0A
+import io.spacedog.utils.Utils;%0A%0A
 public c
@@ -295,16 +295,41 @@
 3601L;%0A%0A
+%09private String message;%0A
 %09private
@@ -620,37 +620,24 @@
 ceException(
-String code, 
 int httpStat
@@ -631,32 +631,49 @@
 (int httpStatus,
+ Throwable cause,
  String message,
@@ -697,52 +697,30 @@
 %7B%0A%09%09
-super(String.format(message, args));%0A%09%09this.
+this(fromStatusToCode(
 http
@@ -717,34 +717,34 @@
 oCode(httpStatus
- =
+),
  httpStatus;%0A%09%09t
@@ -738,36 +738,39 @@
 tpStatus
-;%0A%09%09this.code = code
+, cause, message, args)
 ;%0A%09%7D%0A%0A%09p
@@ -782,32 +782,45 @@
  SpaceException(
+String code, 
 int httpStatus, 
@@ -818,33 +818,16 @@
 pStatus,
- Throwable cause,
  String 
@@ -864,55 +864,42 @@
 his(
-fromStatusToCode(httpStatus), httpStatus, cause
+code, httpStatus, (Throwable) null
 , me
@@ -1025,52 +1025,223 @@
 %7B%0A%09%09
-super(String.format(message, args), cause
+this.message = Utils.isNullOrEmpty(message) //%0A%09%09%09%09? String.format(%22HTTP error %25s%22, httpStatus)%0A%09%09%09%09: String.format(message, args);%0A%0A%09%09if (cause != null)%0A%09%09%09this.message = this.message + %22: %22 + cause.getMessage(
 );%0A
+%0A
 %09%09th
@@ -1284,32 +1284,95 @@
 ode = code;%0A%09%7D%0A%0A
+%09@Override%0A%09public String getMessage() %7B%0A%09%09return message;%0A%09%7D%0A%0A
 %09public int http

Improve default message of SpaceException