Skip to content

Commit

Permalink
Merge pull request #4 from ashtynmo/master
Browse files Browse the repository at this point in the history
Add more specific exceptions for deleteDashboard and getDashboard
  • Loading branch information
ashtynlynne authored Nov 3, 2017
2 parents 6068c94 + 0154ef0 commit 2605b8d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/main/java/com/appnexus/grafana/client/GrafanaClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<GrafanaDashboard> 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());
}
Expand Down Expand Up @@ -126,21 +131,25 @@ 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<DashboardSuccessfulDelete> response =
service.deleteDashboard(apiKey, dashboardName).execute();

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());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 2605b8d

Please sign in to comment.