Only log error if severe logging level is enabled.
This commit is contained in:
parent
d5705c57aa
commit
05195d792d
6 changed files with 140 additions and 103 deletions
9
.idea/bitly-shorten.iml
generated
Normal file
9
.idea/bitly-shorten.iml
generated
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module external.linked.project.id="bitly-shorten" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" type="JAVA_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||||
|
<exclude-output />
|
||||||
|
<content url="file://$MODULE_DIR$" />
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/bitly-shorten.iml" filepath="$PROJECT_DIR$/.idea/bitly-shorten.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -32,6 +32,7 @@
|
||||||
|
|
||||||
package net.thauvin.erik.bitly
|
package net.thauvin.erik.bitly
|
||||||
|
|
||||||
|
import net.thauvin.erik.bitly.Utils.Companion.isSevereLoggable
|
||||||
import net.thauvin.erik.bitly.Utils.Companion.isValidUrl
|
import net.thauvin.erik.bitly.Utils.Companion.isValidUrl
|
||||||
import net.thauvin.erik.bitly.Utils.Companion.removeHttp
|
import net.thauvin.erik.bitly.Utils.Companion.removeHttp
|
||||||
import net.thauvin.erik.bitly.Utils.Companion.toEndPoint
|
import net.thauvin.erik.bitly.Utils.Companion.toEndPoint
|
||||||
|
@ -174,10 +175,12 @@ open class Bitlinks(private val accessToken: String) {
|
||||||
try {
|
try {
|
||||||
parsed = JSONObject(response.body).getString(key, default)
|
parsed = JSONObject(response.body).getString(key, default)
|
||||||
} catch (jse: JSONException) {
|
} catch (jse: JSONException) {
|
||||||
|
if (Utils.logger.isSevereLoggable()) {
|
||||||
Utils.logger.log(Level.SEVERE, "An error occurred parsing the response from Bitly.", jse)
|
Utils.logger.log(Level.SEVERE, "An error occurred parsing the response from Bitly.", jse)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return parsed
|
return parsed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -202,7 +205,9 @@ open class Bitlinks(private val accessToken: String) {
|
||||||
): String {
|
): String {
|
||||||
var bitlink = if (toJson) Constants.EMPTY_JSON else long_url
|
var bitlink = if (toJson) Constants.EMPTY_JSON else long_url
|
||||||
if (!long_url.isValidUrl()) {
|
if (!long_url.isValidUrl()) {
|
||||||
|
if (Utils.logger.isSevereLoggable()) {
|
||||||
Utils.logger.severe("Please specify a valid URL to shorten.")
|
Utils.logger.severe("Please specify a valid URL to shorten.")
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
val params = mutableMapOf("long_url" to long_url).apply {
|
val params = mutableMapOf("long_url" to long_url).apply {
|
||||||
if (group_guid.isNotBlank()) put("group_guid", group_guid)
|
if (group_guid.isNotBlank()) put("group_guid", group_guid)
|
||||||
|
|
|
@ -35,7 +35,7 @@ package net.thauvin.erik.bitly
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.nio.file.Files
|
import java.nio.file.Files
|
||||||
import java.nio.file.Path
|
import java.nio.file.Path
|
||||||
import java.util.Properties
|
import java.util.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provides access to the [Bitly API v4](https://dev.bitly.com/api-reference).
|
* Provides access to the [Bitly API v4](https://dev.bitly.com/api-reference).
|
||||||
|
|
|
@ -50,7 +50,7 @@ import java.util.logging.Logger
|
||||||
open class Utils private constructor() {
|
open class Utils private constructor() {
|
||||||
companion object {
|
companion object {
|
||||||
/** The logger instance. */
|
/** The logger instance. */
|
||||||
val logger: Logger by lazy { Logger.getLogger(Bitly::class.java.simpleName) }
|
val logger: Logger by lazy { Logger.getLogger(Bitly::class.java.name) }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Executes an API call.
|
* Executes an API call.
|
||||||
|
@ -122,6 +122,7 @@ open class Utils private constructor() {
|
||||||
if (!result.isSuccessful && body.isNotEmpty()) {
|
if (!result.isSuccessful && body.isNotEmpty()) {
|
||||||
try {
|
try {
|
||||||
with(JSONObject(body)) {
|
with(JSONObject(body)) {
|
||||||
|
if (logger.isSevereLoggable()) {
|
||||||
if (has("message")) {
|
if (has("message")) {
|
||||||
logger.severe(getString("message") + " (${result.code})")
|
logger.severe(getString("message") + " (${result.code})")
|
||||||
}
|
}
|
||||||
|
@ -129,7 +130,9 @@ open class Utils private constructor() {
|
||||||
logger.severe(getString("description"))
|
logger.severe(getString("description"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} catch (jse: JSONException) {
|
} catch (jse: JSONException) {
|
||||||
|
if (logger.isSevereLoggable()) {
|
||||||
logger.log(
|
logger.log(
|
||||||
Level.SEVERE,
|
Level.SEVERE,
|
||||||
"An error occurred parsing the error response from Bitly. [$endPoint]",
|
"An error occurred parsing the error response from Bitly. [$endPoint]",
|
||||||
|
@ -137,11 +140,17 @@ open class Utils private constructor() {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return body
|
return body
|
||||||
}
|
}
|
||||||
return Constants.EMPTY
|
return Constants.EMPTY
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Is [Level.SEVERE] logging enabled.
|
||||||
|
*/
|
||||||
|
fun Logger.isSevereLoggable(): Boolean = this.isLoggable(Level.SEVERE)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validates a URL.
|
* Validates a URL.
|
||||||
*/
|
*/
|
||||||
|
@ -151,7 +160,9 @@ open class Utils private constructor() {
|
||||||
URL(this)
|
URL(this)
|
||||||
return true
|
return true
|
||||||
} catch (e: MalformedURLException) {
|
} catch (e: MalformedURLException) {
|
||||||
logger.log(Level.FINE, "Invalid URL: $this", e)
|
if (logger.isLoggable(Level.WARNING)) {
|
||||||
|
logger.log(Level.WARNING, "Invalid URL: $this", e)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
@ -177,8 +188,12 @@ open class Utils private constructor() {
|
||||||
|
|
||||||
private fun validateCall(accessToken: String, endPoint: String): Boolean {
|
private fun validateCall(accessToken: String, endPoint: String): Boolean {
|
||||||
when {
|
when {
|
||||||
endPoint.isBlank() -> logger.severe("Please specify a valid API endpoint.")
|
endPoint.isBlank() -> {
|
||||||
accessToken.isBlank() -> logger.severe("Please specify a valid API access token.")
|
if (logger.isSevereLoggable()) logger.severe("Please specify a valid API endpoint.")
|
||||||
|
}
|
||||||
|
accessToken.isBlank() -> {
|
||||||
|
if (logger.isSevereLoggable()) logger.severe("Please specify a valid API access token.")
|
||||||
|
}
|
||||||
else -> return true
|
else -> return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue