Skip to content

Commit

Permalink
优化异常日志打印和ByteArrayWithMimeType类型的日志打印
Browse files Browse the repository at this point in the history
  • Loading branch information
39251474 committed Dec 18, 2021
1 parent 4b462f0 commit 8874eb8
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 12 deletions.
7 changes: 3 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ plugins {
}

android {
compileSdkVersion 30
compileSdkVersion 31

defaultConfig {
applicationId "org.cwk.work.example"
minSdkVersion 19
targetSdkVersion 30
targetSdkVersion 31
versionCode 1
versionName "1.0"
}
Expand Down Expand Up @@ -39,8 +39,7 @@ dependencies {

testImplementation 'junit:junit:4.+'
testImplementation 'org.jetbrains.kotlinx:kotlinx-serialization-json:1.1.0'

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.1"
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2"

implementation project(':work')
}
19 changes: 19 additions & 0 deletions app/src/test/java/org/cwk/work/example/BasicTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class BasicTest {
val work = SimpleErrorWork().start(5)

assertFalse(work.success)
assertEquals(WorkErrorType.RESPONSE, work.errorType)

if (work.success) {
println("work result ${work.result}")
Expand Down Expand Up @@ -152,6 +153,9 @@ class BasicTest {
fun headTest() = runBlocking {
val work = SimpleHeadWork().start()

assertFalse(work.success)
assertEquals(WorkErrorType.RESPONSE, work.errorType)

if (work.success) {
println("work result ${work.result}")
} else {
Expand Down Expand Up @@ -221,6 +225,21 @@ class BasicTest {
val work = SimpleFailedPostWork().start()

assertFalse(work.success)
assertEquals(WorkErrorType.TASK, work.errorType)

if (work.success) {
println("work result :${work.result}")
} else {
println("work error ${work.errorType} message ${work.message}")
}
}

@Test
fun parsedFailedTest() = runBlocking {
val work = SimpleParsedFailedWork().start()

assertFalse(work.success)
assertEquals(WorkErrorType.PARSE, work.errorType)

if (work.success) {
println("work result :${work.result}")
Expand Down
20 changes: 20 additions & 0 deletions app/src/test/java/org/cwk/work/example/SimpleWorks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,24 @@ class SimpleFailedPostWork() : BaseJsonElementWork<Unit>() {
data: WorkData<Unit>,
response: JsonElement
): Unit = Unit
}

/**
* 简单提取结果失败任务
*/
class SimpleParsedFailedWork() : BaseJsonElementWork<String>() {
override fun url() = "/post"

override suspend fun onRequestResult(data: WorkData<String>, response: JsonElement) = true

override fun httpMethod() = HttpMethod.POST

override fun contentType() = MediaType.JSON

override suspend fun fillParams() = Unit

override suspend fun onRequestSuccessful(
data: WorkData<String>,
response: JsonElement
): String = response.jsonArray[0].toString()
}
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.5.21"
ext.kotlin_version = "1.5.31"
repositories {
mavenCentral()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.0'
classpath 'com.android.tools.build:gradle:7.0.4'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
classpath "io.codearte.gradle.nexus:gradle-nexus-staging-plugin:0.30.0"
Expand Down
10 changes: 5 additions & 5 deletions work/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ plugins {
}

android {
compileSdkVersion 30
compileSdkVersion 31

defaultConfig {
minSdkVersion 19
targetSdkVersion 30
versionCode 4
versionName "1.3.0"
targetSdkVersion 31
versionCode 5
versionName "1.4.0"
}

buildTypes {
Expand All @@ -30,7 +30,7 @@ android {

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.1"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2"
api 'com.squareup.okhttp3:okhttp:4.9.1'
}

Expand Down
10 changes: 9 additions & 1 deletion work/src/main/java/org/cwk/work/WorkModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,10 @@ data class ByteArrayWithMimeType(
result = 31 * result + name.hashCode()
return result
}

override fun toString(): String {
return "ByteArrayWithMimeType(name='$name', mimeType='$mimeType', byteArray.size=${byteArray.size})"
}
}

/**
Expand All @@ -371,4 +375,8 @@ internal class WorkException(
val type: WorkErrorType,
message: String? = null,
cause: Throwable? = null
) : Exception(message, cause)
) : Exception(message, cause) {
override fun toString(): String {
return "${javaClass.canonicalName}: type=$type ${message ?: ""}${cause?.let { "\n$it" } ?: ""}"
}
}

0 comments on commit 8874eb8

Please sign in to comment.