Added exception status code and message to examples.
This commit is contained in:
parent
8f08fd738b
commit
a704b955dc
5 changed files with 63 additions and 23 deletions
10
.github/workflows/gradle.yml
vendored
10
.github/workflows/gradle.yml
vendored
|
@ -5,22 +5,28 @@ on: [push, pull_request, workflow_dispatch]
|
|||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
env:
|
||||
GRADLE_OPTS: "-Dorg.gradle.jvmargs=-XX:MaxMetaspaceSize=512m"
|
||||
SONAR_JDK: "11"
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
java-version: [ 11, 15 ]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
with:
|
||||
fetch-depth: 0
|
||||
|
||||
- name: Set up JDK ${{ matrix.java-version }}
|
||||
uses: actions/setup-java@v1
|
||||
with:
|
||||
java-version: ${{ matrix.java-version }}
|
||||
|
||||
- name: Grant execute permission for gradlew
|
||||
run: chmod +x gradlew
|
||||
|
||||
- name: Cache SonarCloud packages
|
||||
if: matrix.java-version == env.SONAR_JDK
|
||||
uses: actions/cache@v1
|
||||
|
@ -28,6 +34,7 @@ jobs:
|
|||
path: ~/.sonar/cache
|
||||
key: ${{ runner.os }}-sonar
|
||||
restore-keys: ${{ runner.os }}-sonar
|
||||
|
||||
- name: Cache Gradle packages
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
|
@ -37,14 +44,17 @@ jobs:
|
|||
key: ${{ runner.os }}-gradle-${{ matrix.java-version }}-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
|
||||
restore-keys: |
|
||||
${{ runner.os }}-gradle-${{ matrix.java-version }}-
|
||||
|
||||
- name: Test with Gradle
|
||||
run: ./gradlew build check --stacktrace
|
||||
|
||||
- name: SonarCloud
|
||||
if: success() && matrix.java-version == env.SONAR_JDK
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
|
||||
run: ./gradlew sonarqube
|
||||
|
||||
- name: Cleanup Gradle Cache
|
||||
run: |
|
||||
rm -f ~/.gradle/caches/modules-2/modules-2.lock
|
||||
|
|
|
@ -11,7 +11,7 @@ plugins {
|
|||
id("java")
|
||||
id("maven-publish")
|
||||
id("org.jetbrains.dokka") version "1.4.32"
|
||||
id("org.sonarqube") version "3.2.0"
|
||||
id("org.sonarqube") version "3.3"
|
||||
id("signing")
|
||||
kotlin("jvm") version "1.5.10"
|
||||
}
|
||||
|
@ -118,8 +118,25 @@ tasks {
|
|||
}
|
||||
}
|
||||
|
||||
val copyToDeploy by registering(Copy::class) {
|
||||
from(configurations.runtimeClasspath) {
|
||||
exclude("annotations-*.jar")
|
||||
}
|
||||
from(jar)
|
||||
into(deployDir)
|
||||
}
|
||||
|
||||
register("deploy") {
|
||||
description = "Copies all needed files to the $deployDir directory."
|
||||
group = PublishingPlugin.PUBLISH_TASK_GROUP
|
||||
dependsOn(build, jar)
|
||||
outputs.dir(deployDir)
|
||||
inputs.files(copyToDeploy)
|
||||
mustRunAfter(clean)
|
||||
}
|
||||
|
||||
"sonarqube" {
|
||||
dependsOn("jacocoTestReport")
|
||||
dependsOn(jacocoTestReport)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -38,7 +38,10 @@ public class CryptoPriceSample {
|
|||
System.out.println("The current " + buyPrice.getBase() + " buy price is " + buyPrice.getAmount()
|
||||
+ " in " + buyPrice.getCurrency());
|
||||
|
||||
} catch (CryptoException | IOException e) {
|
||||
} catch (CryptoException e) {
|
||||
System.err.println("HTTP Status Code: " + e.getStatusCode());
|
||||
System.err.println(e.getMessage());
|
||||
} catch (IOException e) {
|
||||
System.err.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,28 +1,38 @@
|
|||
package com.example
|
||||
|
||||
import net.thauvin.erik.crypto.CryptoException
|
||||
import net.thauvin.erik.crypto.CryptoPrice.Companion.apiCall
|
||||
import net.thauvin.erik.crypto.CryptoPrice.Companion.spotPrice
|
||||
import net.thauvin.erik.crypto.CryptoPrice.Companion.toPrice
|
||||
|
||||
import java.io.IOException
|
||||
|
||||
fun main(@Suppress("UNUSED_PARAMETER") args: Array<String>) {
|
||||
// Get current Bitcoin spot price.
|
||||
val price = spotPrice("BTC")
|
||||
println("The current Bitcoin price is ${price.toCurrency()}")
|
||||
try {
|
||||
// Get current Bitcoin spot price.
|
||||
val price = spotPrice("BTC")
|
||||
println("The current Bitcoin price is ${price.toCurrency()}")
|
||||
|
||||
println()
|
||||
|
||||
// Get current Ethereum spot price in Pound sterling.
|
||||
val gbpPrice = spotPrice("ETH", "GBP")
|
||||
println("The current Ehtereum price is ${gbpPrice.toCurrency()}")
|
||||
println()
|
||||
|
||||
// Get current Ethereum spot price in Pound sterling.
|
||||
val gbpPrice = spotPrice("ETH", "GBP")
|
||||
println("The current Ehtereum price is ${gbpPrice.toCurrency()}")
|
||||
|
||||
// Get current Litecoin spot price in Euro.
|
||||
val euroPrice = spotPrice("LTC", "EUR")
|
||||
println("The current Litecoin price is ${euroPrice.toCurrency()}")
|
||||
// Get current Litecoin spot price in Euro.
|
||||
val euroPrice = spotPrice("LTC", "EUR")
|
||||
println("The current Litecoin price is ${euroPrice.toCurrency()}")
|
||||
|
||||
println()
|
||||
|
||||
// Get current Bitcoin buy price using API.
|
||||
// See: https://developers.coinbase.com/api/v2#get-buy-price
|
||||
val buyPrice = apiCall(listOf("prices", "BTC-USD", "buy"), emptyMap()).toPrice()
|
||||
println("The current ${buyPrice.base} buy price is ${buyPrice.amount} in ${buyPrice.currency}")
|
||||
println()
|
||||
|
||||
// Get current Bitcoin buy price using API.
|
||||
// See: https://developers.coinbase.com/api/v2#get-buy-price
|
||||
val buyPrice = apiCall(listOf("prices", "BTC-USD", "buy"), emptyMap()).toPrice()
|
||||
println("The current ${buyPrice.base} buy price is ${buyPrice.amount} in ${buyPrice.currency}")
|
||||
} catch (e: CryptoException) {
|
||||
System.err.println("HTTP Status Code: ${e.statusCode}")
|
||||
System.err.println(e.message)
|
||||
} catch (e: IOException) {
|
||||
System.err.println(e.message)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* CryptoExtension.kt
|
||||
* CryptoException.kt
|
||||
*
|
||||
* Copyright (c) 2021, Erik C. Thauvin (erik@thauvin.net)
|
||||
* All rights reserved.
|
||||
|
@ -37,7 +37,8 @@ package net.thauvin.erik.crypto
|
|||
*/
|
||||
@Suppress("unused")
|
||||
class CryptoException : Exception {
|
||||
var statusCode = NO_STATUS
|
||||
var statusCode: Int
|
||||
private set
|
||||
|
||||
/** Constructs a new exception with the specified status code, message and cause. */
|
||||
constructor(statusCode: Int = NO_STATUS, message: String, cause: Throwable) : super(message, cause) {
|
||||
|
@ -56,7 +57,6 @@ class CryptoException : Exception {
|
|||
|
||||
companion object {
|
||||
const val NO_STATUS = -1
|
||||
|
||||
private const val serialVersionUID = 1L
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue