Added exception status code and message to examples.

This commit is contained in:
Erik C. Thauvin 2021-06-11 16:53:55 -07:00
parent 8f08fd738b
commit a704b955dc
5 changed files with 63 additions and 23 deletions

View file

@ -5,22 +5,28 @@ on: [push, pull_request, workflow_dispatch]
jobs: jobs:
build: build:
runs-on: ubuntu-latest runs-on: ubuntu-latest
env: env:
GRADLE_OPTS: "-Dorg.gradle.jvmargs=-XX:MaxMetaspaceSize=512m" GRADLE_OPTS: "-Dorg.gradle.jvmargs=-XX:MaxMetaspaceSize=512m"
SONAR_JDK: "11" SONAR_JDK: "11"
strategy: strategy:
matrix: matrix:
java-version: [ 11, 15 ] java-version: [ 11, 15 ]
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
with: with:
fetch-depth: 0 fetch-depth: 0
- name: Set up JDK ${{ matrix.java-version }} - name: Set up JDK ${{ matrix.java-version }}
uses: actions/setup-java@v1 uses: actions/setup-java@v1
with: with:
java-version: ${{ matrix.java-version }} java-version: ${{ matrix.java-version }}
- name: Grant execute permission for gradlew - name: Grant execute permission for gradlew
run: chmod +x gradlew run: chmod +x gradlew
- name: Cache SonarCloud packages - name: Cache SonarCloud packages
if: matrix.java-version == env.SONAR_JDK if: matrix.java-version == env.SONAR_JDK
uses: actions/cache@v1 uses: actions/cache@v1
@ -28,6 +34,7 @@ jobs:
path: ~/.sonar/cache path: ~/.sonar/cache
key: ${{ runner.os }}-sonar key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar restore-keys: ${{ runner.os }}-sonar
- name: Cache Gradle packages - name: Cache Gradle packages
uses: actions/cache@v2 uses: actions/cache@v2
with: with:
@ -37,14 +44,17 @@ jobs:
key: ${{ runner.os }}-gradle-${{ matrix.java-version }}-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} key: ${{ runner.os }}-gradle-${{ matrix.java-version }}-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: | restore-keys: |
${{ runner.os }}-gradle-${{ matrix.java-version }}- ${{ runner.os }}-gradle-${{ matrix.java-version }}-
- name: Test with Gradle - name: Test with Gradle
run: ./gradlew build check --stacktrace run: ./gradlew build check --stacktrace
- name: SonarCloud - name: SonarCloud
if: success() && matrix.java-version == env.SONAR_JDK if: success() && matrix.java-version == env.SONAR_JDK
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
run: ./gradlew sonarqube run: ./gradlew sonarqube
- name: Cleanup Gradle Cache - name: Cleanup Gradle Cache
run: | run: |
rm -f ~/.gradle/caches/modules-2/modules-2.lock rm -f ~/.gradle/caches/modules-2/modules-2.lock

View file

@ -11,7 +11,7 @@ plugins {
id("java") id("java")
id("maven-publish") id("maven-publish")
id("org.jetbrains.dokka") version "1.4.32" id("org.jetbrains.dokka") version "1.4.32"
id("org.sonarqube") version "3.2.0" id("org.sonarqube") version "3.3"
id("signing") id("signing")
kotlin("jvm") version "1.5.10" 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" { "sonarqube" {
dependsOn("jacocoTestReport") dependsOn(jacocoTestReport)
} }
} }

View file

@ -38,7 +38,10 @@ public class CryptoPriceSample {
System.out.println("The current " + buyPrice.getBase() + " buy price is " + buyPrice.getAmount() System.out.println("The current " + buyPrice.getBase() + " buy price is " + buyPrice.getAmount()
+ " in " + buyPrice.getCurrency()); + " 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()); System.err.println(e.getMessage());
} }
} }

View file

@ -1,10 +1,14 @@
package com.example package com.example
import net.thauvin.erik.crypto.CryptoException
import net.thauvin.erik.crypto.CryptoPrice.Companion.apiCall import net.thauvin.erik.crypto.CryptoPrice.Companion.apiCall
import net.thauvin.erik.crypto.CryptoPrice.Companion.spotPrice import net.thauvin.erik.crypto.CryptoPrice.Companion.spotPrice
import net.thauvin.erik.crypto.CryptoPrice.Companion.toPrice import net.thauvin.erik.crypto.CryptoPrice.Companion.toPrice
import java.io.IOException
fun main(@Suppress("UNUSED_PARAMETER") args: Array<String>) { fun main(@Suppress("UNUSED_PARAMETER") args: Array<String>) {
try {
// Get current Bitcoin spot price. // Get current Bitcoin spot price.
val price = spotPrice("BTC") val price = spotPrice("BTC")
println("The current Bitcoin price is ${price.toCurrency()}") println("The current Bitcoin price is ${price.toCurrency()}")
@ -25,4 +29,10 @@ fun main(@Suppress("UNUSED_PARAMETER") args: Array<String>) {
// See: https://developers.coinbase.com/api/v2#get-buy-price // See: https://developers.coinbase.com/api/v2#get-buy-price
val buyPrice = apiCall(listOf("prices", "BTC-USD", "buy"), emptyMap()).toPrice() val buyPrice = apiCall(listOf("prices", "BTC-USD", "buy"), emptyMap()).toPrice()
println("The current ${buyPrice.base} buy price is ${buyPrice.amount} in ${buyPrice.currency}") 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)
}
} }

View file

@ -1,5 +1,5 @@
/* /*
* CryptoExtension.kt * CryptoException.kt
* *
* Copyright (c) 2021, Erik C. Thauvin (erik@thauvin.net) * Copyright (c) 2021, Erik C. Thauvin (erik@thauvin.net)
* All rights reserved. * All rights reserved.
@ -37,7 +37,8 @@ package net.thauvin.erik.crypto
*/ */
@Suppress("unused") @Suppress("unused")
class CryptoException : Exception { class CryptoException : Exception {
var statusCode = NO_STATUS var statusCode: Int
private set
/** Constructs a new exception with the specified status code, message and cause. */ /** Constructs a new exception with the specified status code, message and cause. */
constructor(statusCode: Int = NO_STATUS, message: String, cause: Throwable) : super(message, cause) { constructor(statusCode: Int = NO_STATUS, message: String, cause: Throwable) : super(message, cause) {
@ -56,7 +57,6 @@ class CryptoException : Exception {
companion object { companion object {
const val NO_STATUS = -1 const val NO_STATUS = -1
private const val serialVersionUID = 1L private const val serialVersionUID = 1L
} }
} }