blob: 70a15d175e89bb20575fbe1fe57c3165d2254bfb [file] [log] [blame]
andrewsbrown43b96302015-03-17 21:51:43 +01001/*
2 * jndn-management
3 * Copyright (c) 2015, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU Lesser General Public License,
7 * version 3, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
12 * more details.
13 */
14package com.intel.jndn.management;
15
16import com.intel.jndn.management.types.ControlResponse;
17import net.named_data.jndn.encoding.EncodingException;
18import net.named_data.jndn.util.Blob;
19
20/**
21 * Represent a failure to correctly manage the NDN Forwarding Daemon (NFD).
22 * Inspect this object with getCause() to see why the management operation
23 * failed.
24 *
25 * @author Andrew Brown <andrew.brown@intel.com>
26 */
27public class ManagementException extends Exception {
28
29 /**
30 *
31 * @param message
32 */
33 public ManagementException(String message) {
34 super(message);
35 }
36
37 /**
38 *
39 * @param message
40 * @param cause
41 */
42 public ManagementException(String message, Throwable cause) {
43 super(message, cause);
44 }
45
46 /**
47 * Parse an NFD response to create a ManagementException.
48 *
49 * @param forwarderResponse
50 * @return
51 */
52 public static ManagementException fromResponse(Blob forwarderResponse) {
53 ControlResponse response = new ControlResponse();
54 try {
55 response.wireDecode(forwarderResponse.buf());
56 String message = "Action failed, forwarder returned: " + response.getStatusCode() + " " + response.getStatusText();
57 return new ManagementException(message);
58 } catch (EncodingException e) {
59 return new ManagementException("Action failed and forwarder response was unparseable.", e);
60 }
61 }
62
63 /**
64 * Parse an NFD response to create a ManagementException.
65 *
66 * @param forwarderResponse
67 * @return
68 */
69 public static ManagementException fromResponse(ControlResponse response) {
70 String message = "Action failed, forwarder returned: " + response.getStatusCode() + " " + response.getStatusText();
71 return new ManagementException(message);
72 }
73}