Merge pull request #136 from ultrasonic/error-parser-stability-improvments

Error parser stability improvments
This commit is contained in:
Yahor Berdnikau 2018-01-20 11:15:56 +01:00 committed by GitHub
commit d2f292852d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 106 additions and 9 deletions

View File

@ -113,6 +113,42 @@ class SubsonicApiErrorsTest : SubsonicAPIClientTest() {
response.assertError(RequestedDataWasNotFound)
}
@Test
fun `Should parse error with reversed tokens order`() {
mockWebServerRule.enqueueResponse("reversed_tokens_generic_error.json")
val response = client.api.ping().execute()
response.assertError(Generic("Video streaming not supported"))
}
@Test
fun `Should parse error if json contains error first before other fields`() {
mockWebServerRule.enqueueResponse("error_first_generic_error.json")
val response = client.api.ping().execute()
response.assertError(Generic("Video streaming not supported"))
}
@Test
fun `Should parse error if json doesn't contain message field`() {
mockWebServerRule.enqueueResponse("without_message_generic_error.json")
val response = client.api.ping().execute()
response.assertError(Generic(""))
}
@Test
fun `Should parse error if error json contains additional object`() {
mockWebServerRule.enqueueResponse("with_additional_json_object_generic_error.json")
val response = client.api.ping().execute()
response.assertError(Generic(""))
}
private fun Response<SubsonicResponse>.assertError(expectedError: SubsonicError) =
with(body()) {
error `should not be` null

View File

@ -0,0 +1,10 @@
{
"subsonic-response": {
"error": {
"message": "Video streaming not supported",
"code": 0
},
"version": "1.8.0",
"status": "failed"
}
}

View File

@ -0,0 +1,10 @@
{
"subsonic-response": {
"status": "failed",
"version": "1.8.0",
"error": {
"message": "Video streaming not supported",
"code": 0
}
}
}

View File

@ -0,0 +1,13 @@
{
"subsonic-response": {
"status": "failed",
"version": "1.8.0",
"error": {
"code": 0,
"unicorn" : {
"code": 41,
"message": "Unicorns doesn't exist!"
}
}
}
}

View File

@ -0,0 +1,9 @@
{
"subsonic-response": {
"status": "failed",
"version": "1.8.0",
"error": {
"code": 0
}
}
}

View File

@ -1,6 +1,8 @@
package org.moire.ultrasonic.api.subsonic
import com.fasterxml.jackson.core.JsonParser
import com.fasterxml.jackson.core.JsonToken.END_OBJECT
import com.fasterxml.jackson.core.JsonToken.START_OBJECT
import com.fasterxml.jackson.databind.DeserializationContext
import com.fasterxml.jackson.databind.JsonDeserializer
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
@ -36,13 +38,17 @@ sealed class SubsonicError(val code: Int) {
class SubsonicErrorDeserializer : JsonDeserializer<SubsonicError>() {
override fun deserialize(p: JsonParser, ctxt: DeserializationContext?): SubsonicError {
p.nextToken() // { -> "code"
p.nextToken() // "code" -> codeValue
val code = p.valueAsInt
p.nextToken() // codeValue -> "message"
p.nextToken() // "message" -> messageValue
val message = p.text
p.nextToken() // value -> }
var code = -1
var message = ""
while (p.nextToken() != END_OBJECT) {
when {
p.currentToken == START_OBJECT -> p.skipChildren()
"code".equals(p.currentName, ignoreCase = true) ->
code = p.nextIntValue(-1)
"message".equals(p.currentName, ignoreCase = true) ->
message = p.nextTextValue()
}
}
return getError(code, message)
}
}

View File

@ -20,8 +20,15 @@ import org.moire.ultrasonic.service.SubsonicRESTException
*/
fun SubsonicRESTException.getLocalizedErrorMessage(context: Context): String =
when (error) {
is Generic -> context
.getString(R.string.api_subsonic_generic, (error as Generic).message)
is Generic -> {
val message = (error as Generic).message
val errorMessage = if (message == "") {
context.getString(R.string.api_subsonic_generic_no_message)
} else {
message
}
context.getString(R.string.api_subsonic_generic, errorMessage)
}
RequiredParamMissing -> context.getString(R.string.api_subsonic_param_missing)
IncompatibleClientProtocolVersion -> context
.getString(R.string.api_subsonic_upgrade_client)

View File

@ -424,6 +424,7 @@
<!-- Subsonic api errors -->
<string name="api.subsonic.generic">Error genérico de api: %1$s</string>
<string name="api.subsonic.generic.no.message">ningún mensaje dado desde el servidor</string>
<string name="api.subsonic.token_auth_not_supported_for_ldap">La autenticación por token no es compatible con usuarios LDAP.</string>
<string name="api.subsonic.not_authenticated">Nombre de usuario o contraseña incorrectos.</string>
<string name="api.subsonic.not_authorized">No autorizado. Comprueba los permisos de usuario en el servidor de Subsonic.</string>

View File

@ -424,6 +424,7 @@
<!-- Subsonic api errors -->
<string name="api.subsonic.generic">Erreur api générique: %1$s</string>
<string name="api.subsonic.generic.no.message">aucun message donné par le serveur</string>
<string name="api.subsonic.token_auth_not_supported_for_ldap">L\'authentification par jeton n\'est pas prise en charge pour les utilisateurs LDAP.</string>
<string name="api.subsonic.not_authenticated">Mauvais nom d\'usager ou mot de passe.</string>
<string name="api.subsonic.not_authorized">Non autorisé. Vérifiez les permissions de l\'utilisateur dans le serveur Subsonic.</string>

View File

@ -424,6 +424,7 @@
<!-- Subsonic api errors -->
<string name="api.subsonic.generic">Általános api hiba: %1$s</string>
<string name="api.subsonic.generic.no.message">nincs üzenet a szerverről</string>
<string name="api.subsonic.token_auth_not_supported_for_ldap">Az LDAP-felhasználók számára nem támogatott a token-hitelesítés.</string>
<string name="api.subsonic.not_authenticated">Hibás felhasználónév vagy jelszó!</string>
<string name="api.subsonic.not_authorized">Nem engedélyezett! Ellenőrizze a felhasználó jogosultságait a Subsonic kiszolgálón!</string>

View File

@ -424,6 +424,7 @@
<!-- Subsonic api errors -->
<string name="api.subsonic.generic">Erro de api genérico: %1$s</string>
<string name="api.subsonic.generic.no.message">nenhuma mensagem fornecida pelo servidor</string>
<string name="api.subsonic.token_auth_not_supported_for_ldap">A autenticação por token não é suportada para usuários LDAP.</string>
<string name="api.subsonic.not_authenticated">Login ou senha errada.</string>
<string name="api.subsonic.not_authorized">Não autorizado. Verifique as permissões do usuário no servidor Subsonic.</string>

View File

@ -424,6 +424,7 @@
<!-- Subsonic api errors -->
<string name="api.subsonic.generic">Erro de api genérico: %1$s</string>
<string name="api.subsonic.generic.no.message">nenhuma mensagem fornecida pelo servidor</string>
<string name="api.subsonic.token_auth_not_supported_for_ldap">A autenticação por token não é suportada para usuários LDAP.</string>
<string name="api.subsonic.not_authenticated">Login ou senha errada.</string>
<string name="api.subsonic.not_authorized">Não autorizado. Verifique as permissões do usuário no servidor Subsonic.</string>

View File

@ -426,6 +426,7 @@
<!-- Subsonic api errors -->
<string name="api.subsonic.generic">Generic api error: %1$s</string>
<string name="api.subsonic.generic.no.message">no message given from server</string>
<string name="api.subsonic.token_auth_not_supported_for_ldap">Authentication by token is not supported for LDAP users.</string>
<string name="api.subsonic.not_authenticated">Wrong username or password.</string>
<string name="api.subsonic.not_authorized">Not authorized. Check user permissions in Subsonic server.</string>