Changeset View
Changeset View
Standalone View
Standalone View
src/main/java/net/wildfyre/http/IssueInTransferException.kt
Show All 12 Lines | |||||
* See the License for the specific language governing permissions and | * See the License for the specific language governing permissions and | ||||
* limitations under the License. | * limitations under the License. | ||||
*/ | */ | ||||
package net.wildfyre.http | package net.wildfyre.http | ||||
import com.eclipsesource.json.Json | import com.eclipsesource.json.Json | ||||
import com.eclipsesource.json.JsonValue | import com.eclipsesource.json.JsonValue | ||||
import com.eclipsesource.json.ParseException | |||||
import com.eclipsesource.json.WriterConfig | import com.eclipsesource.json.WriterConfig | ||||
import net.wildfyre.descriptors.NoSuchEntityException | import net.wildfyre.descriptors.NoSuchEntityException | ||||
import java.io.IOException | import java.io.IOException | ||||
import java.io.InputStream | import java.io.InputStream | ||||
import java.io.InputStreamReader | import java.io.InputStreamReader | ||||
/** | /** | ||||
* Signifies that the server refused the data. More information about the problem can be found using [json]. | * Signifies that the server refused the data. More information about the problem can be found using [json]. | ||||
*/ | */ | ||||
class IssueInTransferException : IOException { | class IssueInTransferException : IOException { | ||||
/** | /** | ||||
* The JSON data sent by the server (if any). | * The JSON data sent by the server (if any). | ||||
*/ | */ | ||||
var json: JsonValue? = null | var json: JsonValue? = null | ||||
private set | private set | ||||
var msg: String? = null | |||||
private set | |||||
/** | /** | ||||
* Creates a new IssueInTransferException. | * Creates a new IssueInTransferException. | ||||
* @param msg the message | * @param message the message | ||||
* @param input the response from the server, that will be parsed as JSON if possible | * @param input the response from the server, that will be parsed as JSON if possible | ||||
*/ | */ | ||||
constructor(msg: String, input: InputStream) : super(msg) { | constructor(message: String, input: InputStream) : super(message) { | ||||
try { | |||||
json = Json.parse(InputStreamReader(input, Request.CHARSET)) | json = Json.parse(InputStreamReader(input, Request.CHARSET)) | ||||
} catch (e: ParseException) { | |||||
this.msg = input.reader(Request.CHARSET).use { it.readText() } | |||||
} | |||||
} | } | ||||
/** | /** | ||||
* Creates a new IssueInTransferException. | * Creates a new IssueInTransferException. | ||||
* @param msg the message | * @param msg the message | ||||
* @param cause what caused the issue | * @param cause what caused the issue | ||||
*/ | */ | ||||
constructor(msg: String, cause: Exception) : super(msg, cause) { | constructor(msg: String, cause: Exception) : super(msg, cause) { | ||||
Show All 29 Lines | interface ExceptionRunnable { | ||||
* The action of this object. | * The action of this object. | ||||
* @throws NoSuchEntityException If there's a missing entity at some point. | * @throws NoSuchEntityException If there's a missing entity at some point. | ||||
*/ | */ | ||||
@Throws(NoSuchEntityException::class) | @Throws(NoSuchEntityException::class) | ||||
fun run() | fun run() | ||||
} | } | ||||
override val message: String? | override val message: String? | ||||
get() = if (json != null) | get() = "${super.message}\n" + when { | ||||
"${super.message}\n${json!!.toString(WriterConfig.PRETTY_PRINT)}" | json != null -> json!!.toString(WriterConfig.PRETTY_PRINT) | ||||
else | msg != null -> msg!! | ||||
super.message | else -> "" | ||||
} | |||||
} | } |