From 3dd16fe9ea1cd28378fc3f82fa98e18c4f54541a Mon Sep 17 00:00:00 2001
From: Lance Minecraft <98957643+LanceMinecraftFBA@users.noreply.github.com>
Date: Mon, 6 Mar 2023 14:48:15 +0300
Subject: [PATCH] Add Exceptions
---
OutlineManager.cs | 68 ++++++++++++++++++++++++++++++++++++++++++-----
1 file changed, 62 insertions(+), 6 deletions(-)
diff --git a/OutlineManager.cs b/OutlineManager.cs
index 5e3ecfd..3dc6743 100644
--- a/OutlineManager.cs
+++ b/OutlineManager.cs
@@ -7,6 +7,7 @@
using OutlineManager.Types;
using Newtonsoft.Json.Linq;
using System.Linq;
+using OutlineManagerExceptions;
namespace OutlineManager
{
@@ -20,7 +21,6 @@ public class Outline
/// Welcome to OutlineAPI Manager!
///
/// URL for access to Management API
-
public Outline(string apiUrl)
{
ApiUrl = apiUrl;
@@ -53,10 +53,17 @@ private static bool CallRequest(string urlMethod, string method, out string data
}
return true;
}
- catch
+ catch(WebException exc)
{
data = null;
- return false;
+ if (exc.Message == "The SSL connection could not be established, see inner exception.")
+ throw new OutlineAPIException("Your Outline Server hasn't SSL connection, please set hasSsl = false");
+ else if (exc.Message.Contains($"({ApiUrl}:443)"))
+ throw new OutlineManagerException("Server not found, please check API URL");
+ else if (exc.Message.Contains("(404) Not Found"))
+ throw new OutlineAPIException("Server returned 404: key not found");
+ else
+ throw new OutlineAPIException("Unknown Exception, please check API URL and see inner exception", exc);
}
}
private static bool CallRequest(string urlMethod, string method, JObject args, out string data)
@@ -65,16 +72,66 @@ private static bool CallRequest(string urlMethod, string method, JObject args, o
{
WebClient webClient = new WebClient();
webClient.Headers[HttpRequestHeader.ContentType] = "application/json";
+ if(HasSsl == false)
+ ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
data = webClient.UploadString($"{ApiUrl}/{urlMethod}", method, args.ToString());
return true;
}
- catch
+ catch(WebException exc)
{
data = null;
- return false;
+ if (exc.Message == "The SSL connection could not be established, see inner exception.")
+ throw new OutlineAPIException("Your Outline Server hasn't SSL connection, please set hasSsl = false");
+ else if (exc.Message.Contains($"({ApiUrl}:443)"))
+ throw new OutlineManagerException("Server not found, please check API URL");
+ else if (exc.Message.Contains("(404) Not Found"))
+ throw new OutlineAPIException("Server returned 404: key not found");
+ else
+ throw new OutlineAPIException("Unknown Exception, please check API URL and see inner exception", exc);
}
}
+ ///
+ /// Get Outline key in by ID
+ ///
+ /// ID of key
+ /// Key in
+ ///
+ public OutlineKey GetKeyById(int id)
+ {
+ var list = GetKeys();
+ OutlineKey oKey = new OutlineKey() { Id = -1 };
+ foreach (var key in list)
+ if (key.Id == id)
+ oKey = key;
+ if (oKey.Id == -1)
+ throw new OutlineManagerException("Key not exist in your Outline Server");
+ else
+ return oKey;
+
+ }
+ ///
+ /// Get Outline key in by Name.
Avaiable methods:
- Equality by Name
- Equality by Name with Lower Case
- Starts with name with Lower Case
+ ///
+ /// Name of key
+ ///
+ ///
+ public OutlineKey GetKeyByName(string name)
+ {
+ var list = GetKeys();
+ OutlineKey oKey = new OutlineKey() { Id = -1 };
+ foreach (var key in list)
+ if (key.Name.ToLower() == name.ToLower())
+ oKey = key;
+ else if(key.Name == name)
+ oKey = key;
+ else if (key.Name.ToLower().StartsWith(name.ToLower()))
+ oKey = key;
+ if (oKey.Id == -1)
+ throw new OutlineManagerException("Key not exist or not found by name in your Outline Server");
+ else
+ return oKey;
+ }
///
/// Get Outline Keys from Outline Server
///
@@ -166,4 +223,3 @@ public List GetTransferredData()
}
}
}
-