Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Commit

Permalink
Wrap exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
mtwalli committed Apr 6, 2021
1 parent 1937040 commit d1c97a4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package de.rki.coronawarnapp.eventregistration.checkins.qrcode

sealed class QRCodeException constructor(message: String? = null) : Exception(message)
sealed class QRCodeException constructor(
message: String? = null,
cause: Throwable? = null
) : Exception(message, cause)

class InvalidQrCodeUriException constructor(message: String? = null) : QRCodeException(message)
class InvalidQrCodeDataException constructor(message: String? = null) : QRCodeException(message)

class InvalidQrCodePayloadException constructor(message: String? = null) : QRCodeException(message)

class InvalidQrCodeDataException constructor(
message: String? = null, cause: Throwable? = null
) : QRCodeException(message, cause)
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package de.rki.coronawarnapp.eventregistration.checkins.qrcode

import com.google.common.io.BaseEncoding
import dagger.Reusable
import de.rki.coronawarnapp.appconfig.AppConfigProvider
import de.rki.coronawarnapp.server.protocols.internal.pt.TraceLocationOuterClass.QRCodePayload
import de.rki.coronawarnapp.server.protocols.internal.v2.PresenceTracingParametersOuterClass.PresenceTracingQRCodeDescriptorOrBuilder
import de.rki.coronawarnapp.server.protocols.internal.v2.PresenceTracingParametersOuterClass.PresenceTracingQRCodeDescriptor.PayloadEncoding
import de.rki.coronawarnapp.util.decodeBase32
import okio.ByteString.Companion.decodeBase64
import okio.ByteString.Companion.toByteString
import timber.log.Timber
import java.net.URI
import javax.inject.Inject
Expand All @@ -26,7 +28,12 @@ class QRCodeUriParser @Inject constructor(
@Suppress("BlockingMethodInNonBlockingContext")
suspend fun getQrCodePayload(input: String): QRCodePayload {
Timber.d("input=$input")
URI.create(input) // Verify it is a valid uri
try {
URI.create(input) // Verify it is a valid uri
} catch (e: Exception) {
Timber.d(e, "Invalid URI")
throw InvalidQrCodeUriException("Invalid URI")
}

val descriptor = descriptor(input)
val groups = descriptor.matchedGroups(input)
Expand All @@ -37,10 +44,15 @@ class QRCodeUriParser @Inject constructor(
val encoding = PayloadEncoding.forNumber(descriptor.payloadEncoding.number)
Timber.d("encoding=$encoding")

val rawPayload = when (encoding) {
PayloadEncoding.BASE32 -> payload.decodeBase32()
PayloadEncoding.BASE64 -> payload.decodeBase64()
else -> null
val rawPayload = try {
when (encoding) {
PayloadEncoding.BASE32 -> payload.decodeBase32()
PayloadEncoding.BASE64 -> BaseEncoding.base64Url().decode(payload).toByteString()
else -> null
}
} catch (e: Exception) {
Timber.d(e, "Payload decoding failed")
throw InvalidQrCodeDataException("Payload decoding failed", e)
} ?: throw InvalidQrCodeDataException("Payload decoding failed")

return QRCodePayload.parseFrom(rawPayload.toByteArray())
Expand Down

0 comments on commit d1c97a4

Please sign in to comment.