From 0154ef0f0e0ae94a4b09a8623a945bb639fe55c4 Mon Sep 17 00:00:00 2001 From: Ashtyn Edwards Date: Thu, 2 Nov 2017 16:03:58 -0700 Subject: [PATCH] Add more specific exceptions for deleteDashboard and getDashboard --- .../grafana/client/GrafanaClient.java | 25 +++++++++++++------ ...afanaDashboardCouldNotDeleteException.java | 23 +++++++++++++++++ ...GrafanaDashboardDoesNotExistException.java | 23 +++++++++++++++++ 3 files changed, 63 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/appnexus/grafana/exceptions/GrafanaDashboardCouldNotDeleteException.java create mode 100644 src/main/java/com/appnexus/grafana/exceptions/GrafanaDashboardDoesNotExistException.java diff --git a/src/main/java/com/appnexus/grafana/client/GrafanaClient.java b/src/main/java/com/appnexus/grafana/client/GrafanaClient.java index 32deeee..a76a3cd 100644 --- a/src/main/java/com/appnexus/grafana/client/GrafanaClient.java +++ b/src/main/java/com/appnexus/grafana/client/GrafanaClient.java @@ -10,6 +10,8 @@ import com.appnexus.grafana.client.models.GrafanaDashboard; import com.appnexus.grafana.client.models.GrafanaMessage; import com.appnexus.grafana.configuration.GrafanaConfiguration; +import com.appnexus.grafana.exceptions.GrafanaDashboardCouldNotDeleteException; +import com.appnexus.grafana.exceptions.GrafanaDashboardDoesNotExistException; import com.appnexus.grafana.exceptions.GrafanaException; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.JsonInclude; @@ -71,17 +73,20 @@ public String getHost() { * * @param dashboardName the name of the dashboard to search for. * @return {@link GrafanaDashboard} with matching name. - * @throws GrafanaException if a dashboard with matching name does not exist. + * @throws GrafanaDashboardDoesNotExistException if a dashboard with matching name does not exist. + * @throws GrafanaException if Grafana returns an error when trying to retrieve the dashboard. * @throws IOException if a problem occurred talking to the server. */ - public GrafanaDashboard getDashboard(String dashboardName) throws GrafanaException, IOException { + public GrafanaDashboard getDashboard(String dashboardName) + throws GrafanaDashboardDoesNotExistException, GrafanaException, IOException { Response response = service.getDashboard(apiKey, dashboardName).execute(); if (response.isSuccessful()) { return response.body(); } else if (response.code() == HTTP_NOT_FOUND) { - throw new GrafanaException("Dashboard " + dashboardName + " does not exist"); + throw new GrafanaDashboardDoesNotExistException( + "Dashboard " + dashboardName + " does not exist"); } else { throw GrafanaException.withErrorBody(response.errorBody()); } @@ -126,11 +131,14 @@ public DashboardMeta updateDashboard(GrafanaDashboard dashboard) * * @param dashboardName the name of the dashboard to delete. * @return The name of the deleted dashboard. - * @throws GrafanaException if the dashboard does not exist or Grafana returns an error when - * trying to delete the dashboard. + * @throws GrafanaDashboardDoesNotExistException if the dashboard does not exist + * @throws GrafanaDashboardCouldNotDeleteException if Grafana returns an error when trying to + * delete the dashboard. * @throws IOException if a problem occurred talking to the server. */ - public String deleteDashboard(String dashboardName) throws GrafanaException, IOException { + public String deleteDashboard(String dashboardName) + throws GrafanaDashboardDoesNotExistException, GrafanaDashboardCouldNotDeleteException, + IOException { Response response = service.deleteDashboard(apiKey, dashboardName).execute(); @@ -138,9 +146,10 @@ public String deleteDashboard(String dashboardName) throws GrafanaException, IOE if (response.isSuccessful()) { return response.body().title(); } else if (response.code() == HTTP_NOT_FOUND) { - throw new GrafanaException("Dashboard " + dashboardName + " does not exist"); + throw new GrafanaDashboardDoesNotExistException( + "Dashboard " + dashboardName + " does not exist"); } else { - throw GrafanaException.withErrorBody(response.errorBody()); + throw GrafanaDashboardCouldNotDeleteException.withErrorBody(response.errorBody()); } } diff --git a/src/main/java/com/appnexus/grafana/exceptions/GrafanaDashboardCouldNotDeleteException.java b/src/main/java/com/appnexus/grafana/exceptions/GrafanaDashboardCouldNotDeleteException.java new file mode 100644 index 0000000..94bd2e0 --- /dev/null +++ b/src/main/java/com/appnexus/grafana/exceptions/GrafanaDashboardCouldNotDeleteException.java @@ -0,0 +1,23 @@ +/* Licensed under Apache-2.0 */ +package com.appnexus.grafana.exceptions; + +import java.io.IOException; +import okhttp3.ResponseBody; + +public class GrafanaDashboardCouldNotDeleteException extends GrafanaException { + + public static GrafanaDashboardCouldNotDeleteException withErrorBody(ResponseBody body) + throws IOException { + return body != null + ? new GrafanaDashboardCouldNotDeleteException("Unexpected Grafana error; " + body.string()) + : new GrafanaDashboardCouldNotDeleteException("Unexpected Grafana error"); + } + + public GrafanaDashboardCouldNotDeleteException(String message) { + super(message); + } + + public GrafanaDashboardCouldNotDeleteException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/com/appnexus/grafana/exceptions/GrafanaDashboardDoesNotExistException.java b/src/main/java/com/appnexus/grafana/exceptions/GrafanaDashboardDoesNotExistException.java new file mode 100644 index 0000000..d54379e --- /dev/null +++ b/src/main/java/com/appnexus/grafana/exceptions/GrafanaDashboardDoesNotExistException.java @@ -0,0 +1,23 @@ +/* Licensed under Apache-2.0 */ +package com.appnexus.grafana.exceptions; + +import java.io.IOException; +import okhttp3.ResponseBody; + +public class GrafanaDashboardDoesNotExistException extends GrafanaException { + + public static GrafanaDashboardDoesNotExistException withErrorBody(ResponseBody body) + throws IOException { + return body != null + ? new GrafanaDashboardDoesNotExistException("Unexpected Grafana error; " + body.string()) + : new GrafanaDashboardDoesNotExistException("Unexpected Grafana error"); + } + + public GrafanaDashboardDoesNotExistException(String message) { + super(message); + } + + public GrafanaDashboardDoesNotExistException(String message, Throwable cause) { + super(message, cause); + } +}