-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(backend): Add more tests and improve response codes
- Loading branch information
Showing
9 changed files
with
145 additions
and
41 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.grohden.repotagger.api | ||
|
||
import io.ktor.application.ApplicationCall | ||
import io.ktor.request.ContentTransformationException | ||
import io.ktor.request.receive | ||
|
||
|
||
class BadRequest( | ||
message: String | ||
) : RuntimeException(message) | ||
|
||
|
||
class NotFound( | ||
message: String | ||
) : RuntimeException(message) | ||
|
||
/** | ||
* Requires a parameter if it's | ||
* not present throw [BadRequest] | ||
*/ | ||
fun ApplicationCall.requireParam(key: String): String { | ||
return parameters[key] ?: throw BadRequest("Arg $key is invalid") | ||
} | ||
|
||
/** | ||
* Requires a parameter to be non null and int, otherwise | ||
* throws [BadRequest] | ||
*/ | ||
fun ApplicationCall.requireIntParam(key: String): Int { | ||
return try { | ||
requireParam(key).toInt() | ||
} catch (error: NumberFormatException) { | ||
throw BadRequest("Arg $key is invalid") | ||
} | ||
} | ||
|
||
/** | ||
* Requires a parameter to be non null and int, otherwise | ||
* throws [BadRequest] | ||
*/ | ||
suspend inline fun <reified T : Any> ApplicationCall.requireReceived(): T { | ||
return try { | ||
receive() | ||
} catch (error: ContentTransformationException) { | ||
throw BadRequest("Could not parse body contents") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters