fix: resolved issue with GetTypes request handlers causing a 500 error response; updated debug constants to default levels

This commit is contained in:
jdevito
2023-02-01 09:43:52 -06:00
parent c68d46f971
commit 15efed02a5
14 changed files with 122 additions and 87 deletions

View File

@@ -212,9 +212,9 @@ namespace PepperDash.Essentials.Core
/// <returns></returns> /// <returns></returns>
public static Dictionary<string, DeviceFactoryWrapper> GetDeviceFactoryDictionary(string filter) public static Dictionary<string, DeviceFactoryWrapper> GetDeviceFactoryDictionary(string filter)
{ {
return !string.IsNullOrEmpty(filter) return string.IsNullOrEmpty(filter)
? FactoryMethods.Where(k => k.Key.Contains(filter)).ToDictionary(k => k.Key, k => k.Value) ? FactoryMethods
: FactoryMethods; : FactoryMethods.Where(k => k.Key.Contains(filter)).ToDictionary(k => k.Key, k => k.Value);
} }
} }
} }

View File

@@ -11,8 +11,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
{ {
private const string Key = "AppDebugRequestHandler"; private const string Key = "AppDebugRequestHandler";
private const uint Trace = 0; private const uint Trace = 0;
private const uint Info = 0; private const uint Info = 1;
private const uint Verbose = 0; private const uint Verbose = 2;
/// <summary> /// <summary>
/// Handles CONNECT method requests /// Handles CONNECT method requests

View File

@@ -16,8 +16,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
/// <param name="context"></param> /// <param name="context"></param>
protected override void HandleConnect(HttpCwsContext context) protected override void HandleConnect(HttpCwsContext context)
{ {
context.Response.StatusCode = 501; context.Response.StatusCode = 418;
context.Response.StatusDescription = "Not Implemented"; context.Response.StatusDescription = "I'm a teapot";
context.Response.End(); context.Response.End();
} }
@@ -27,8 +27,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
/// <param name="context"></param> /// <param name="context"></param>
protected override void HandleDelete(HttpCwsContext context) protected override void HandleDelete(HttpCwsContext context)
{ {
context.Response.StatusCode = 501; context.Response.StatusCode = 418;
context.Response.StatusDescription = "Not Implemented"; context.Response.StatusDescription = "I'm a teapot";
context.Response.End(); context.Response.End();
} }
@@ -38,8 +38,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
/// <param name="context"></param> /// <param name="context"></param>
protected override void HandleGet(HttpCwsContext context) protected override void HandleGet(HttpCwsContext context)
{ {
context.Response.StatusCode = 501; context.Response.StatusCode = 418;
context.Response.StatusDescription = "Not Implemented"; context.Response.StatusDescription = "I'm a teapot";
context.Response.End(); context.Response.End();
} }
@@ -49,8 +49,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
/// <param name="context"></param> /// <param name="context"></param>
protected override void HandleHead(HttpCwsContext context) protected override void HandleHead(HttpCwsContext context)
{ {
context.Response.StatusCode = 501; context.Response.StatusCode = 418;
context.Response.StatusDescription = "Not Implemented"; context.Response.StatusDescription = "I'm a teapot";
context.Response.End(); context.Response.End();
} }
@@ -60,8 +60,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
/// <param name="context"></param> /// <param name="context"></param>
protected override void HandleOptions(HttpCwsContext context) protected override void HandleOptions(HttpCwsContext context)
{ {
context.Response.StatusCode = 501; context.Response.StatusCode = 418;
context.Response.StatusDescription = "Not Implemented"; context.Response.StatusDescription = "I'm a teapot";
context.Response.End(); context.Response.End();
} }
@@ -71,8 +71,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
/// <param name="context"></param> /// <param name="context"></param>
protected override void HandlePatch(HttpCwsContext context) protected override void HandlePatch(HttpCwsContext context)
{ {
context.Response.StatusCode = 501; context.Response.StatusCode = 418;
context.Response.StatusDescription = "Not Implemented"; context.Response.StatusDescription = "I'm a teapot";
context.Response.End(); context.Response.End();
} }
@@ -82,8 +82,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
/// <param name="context"></param> /// <param name="context"></param>
protected override void HandlePost(HttpCwsContext context) protected override void HandlePost(HttpCwsContext context)
{ {
context.Response.StatusCode = 501; context.Response.StatusCode = 418;
context.Response.StatusDescription = "Not Implemented"; context.Response.StatusDescription = "I'm a teapot";
context.Response.End(); context.Response.End();
} }
@@ -93,8 +93,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
/// <param name="context"></param> /// <param name="context"></param>
protected override void HandlePut(HttpCwsContext context) protected override void HandlePut(HttpCwsContext context)
{ {
context.Response.StatusCode = 501; context.Response.StatusCode = 418;
context.Response.StatusDescription = "Not Implemented"; context.Response.StatusDescription = "I'm a teapot";
context.Response.End(); context.Response.End();
} }
@@ -104,8 +104,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
/// <param name="context"></param> /// <param name="context"></param>
protected override void HandleTrace(HttpCwsContext context) protected override void HandleTrace(HttpCwsContext context)
{ {
context.Response.StatusCode = 501; context.Response.StatusCode = 418;
context.Response.StatusDescription = "Not Implemented"; context.Response.StatusDescription = "I'm a teapot";
context.Response.End(); context.Response.End();
} }
} }

View File

@@ -10,8 +10,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
{ {
private const string Key = "DevJsonRequestHandler"; private const string Key = "DevJsonRequestHandler";
private const uint Trace = 0; private const uint Trace = 0;
private const uint Info = 0; private const uint Info = 1;
private const uint Verbose = 0; private const uint Verbose = 2;
/// <summary> /// <summary>
/// Handles CONNECT method requests /// Handles CONNECT method requests

View File

@@ -10,8 +10,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
{ {
private const string Key = "DevListRequestHandler"; private const string Key = "DevListRequestHandler";
private const uint Trace = 0; private const uint Trace = 0;
private const uint Info = 0; private const uint Info = 1;
private const uint Verbose = 0; private const uint Verbose = 2;
/// <summary> /// <summary>
/// Handles CONNECT method requests /// Handles CONNECT method requests

View File

@@ -11,8 +11,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
{ {
private const string Key = "DevPropsRequestHandler"; private const string Key = "DevPropsRequestHandler";
private const uint Trace = 0; private const uint Trace = 0;
private const uint Info = 0; private const uint Info = 1;
private const uint Verbose = 0; private const uint Verbose = 2;
/// <summary> /// <summary>
/// Handles CONNECT method requests /// Handles CONNECT method requests

View File

@@ -5,6 +5,11 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
{ {
public class DisableAllStreamDebugRequestHandler : WebApiBaseRequestHandler public class DisableAllStreamDebugRequestHandler : WebApiBaseRequestHandler
{ {
private const string Key = "DisableAllStreamDebugRequestHandler";
private const uint Trace = 0;
private const uint Info = 1;
private const uint Verbose = 2;
/// <summary> /// <summary>
/// Handles CONNECT method requests /// Handles CONNECT method requests
/// </summary> /// </summary>

View File

@@ -10,8 +10,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
{ {
private const string Key = "GetJoinMapForBridgeKeyRequestHandler"; private const string Key = "GetJoinMapForBridgeKeyRequestHandler";
private const uint Trace = 0; private const uint Trace = 0;
private const uint Info = 0; private const uint Info = 1;
private const uint Verbose = 0; private const uint Verbose = 2;
/// <summary> /// <summary>
/// Handles CONNECT method requests /// Handles CONNECT method requests

View File

@@ -11,8 +11,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
{ {
private const string Key = "GetJoinMapForDeviceKeyRequestHandler"; private const string Key = "GetJoinMapForDeviceKeyRequestHandler";
private const uint Trace = 0; private const uint Trace = 0;
private const uint Info = 0; private const uint Info = 1;
private const uint Verbose = 0; private const uint Verbose = 2;
/// <summary> /// <summary>
/// Handles CONNECT method requests /// Handles CONNECT method requests

View File

@@ -1,8 +1,9 @@
using Crestron.SimplSharp.WebScripting; using System;
using System.Linq;
using Crestron.SimplSharp.WebScripting;
using Newtonsoft.Json; using Newtonsoft.Json;
using PepperDash.Core; using PepperDash.Core;
using PepperDash.Core.Web.RequestHandlers; using PepperDash.Core.Web.RequestHandlers;
using PepperDash.Essentials.Core.Bridges;
namespace PepperDash.Essentials.Core.Web.RequestHandlers namespace PepperDash.Essentials.Core.Web.RequestHandlers
{ {
@@ -10,8 +11,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
{ {
private const string Key = "GetTypesByFilterRequestHandler"; private const string Key = "GetTypesByFilterRequestHandler";
private const uint Trace = 0; private const uint Trace = 0;
private const uint Info = 0; private const uint Info = 1;
private const uint Verbose = 0; private const uint Verbose = 2;
/// <summary> /// <summary>
/// Handles CONNECT method requests /// Handles CONNECT method requests
@@ -51,10 +52,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
return; return;
} }
var routeDataJson = JsonConvert.SerializeObject(routeData, Formatting.Indented); var routeDataJson = JsonConvert.SerializeObject(routeData, Formatting.Indented);
Debug.Console(Verbose, "routeData:\n{0}", routeDataJson); Debug.Console(Verbose, "[{0}] routeData:\n{1}", Key.ToLower(), routeDataJson);
object filterObj; object filterObj;
if (!routeData.Values.TryGetValue("filter", out filterObj)) if (!routeData.Values.TryGetValue("filter", out filterObj))
@@ -68,11 +67,15 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
return; return;
} }
var types = DeviceFactory.GetDeviceFactoryDictionary(filterObj.ToString()); var types = DeviceFactory.GetDeviceFactoryDictionary(filterObj.ToString()).Select(type => new
{
Type = type.Key,
Description = type.Value.Description,
CType = type.Value.CType == null ? "---" : type.Value.CType.ToString()
}).Cast<object>().ToList();
if (types == null) if (types == null)
{ {
Debug.Console(Verbose, "Get device type from dictionary failed");
context.Response.StatusCode = 400; context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request"; context.Response.StatusDescription = "Bad Request";
context.Response.End(); context.Response.End();
@@ -80,22 +83,28 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
return; return;
} }
var js = JsonConvert.SerializeObject(types, Formatting.Indented, new JsonSerializerSettings try
{ {
ReferenceLoopHandling = ReferenceLoopHandling.Ignore, var js = JsonConvert.SerializeObject(types, Formatting.Indented);
NullValueHandling = NullValueHandling.Ignore, //Debug.Console(Verbose, "[{0}] HandleGet: \x0d\x0a{1}", Key.ToLower(), js);
MissingMemberHandling = MissingMemberHandling.Ignore,
DefaultValueHandling = DefaultValueHandling.Ignore,
TypeNameHandling = TypeNameHandling.None
});
Debug.Console(Verbose, "[{0}] HandleGet: \x0d\x0a{1}", Key.ToLower(), js);
context.Response.StatusCode = 200; context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK"; context.Response.StatusDescription = "OK";
context.Response.ContentType = "application/json"; context.Response.ContentType = "application/json";
context.Response.ContentEncoding = System.Text.Encoding.UTF8; context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.Write(js, false); context.Response.Write(js, false);
context.Response.End(); context.Response.End();
}
catch (Exception ex)
{
Debug.Console(Info, "[{0}] HandleGet Exception Message: {1}", Key.ToLower(), ex.Message);
Debug.Console(Verbose, "[{0}] HandleGet Exception StackTrace: {1}", Key.ToLower(), ex.StackTrace);
if (ex.InnerException != null) Debug.Console(Verbose, "[{0}] HandleGet Exception InnerException: {1}", Key.ToLower(), ex.InnerException);
context.Response.StatusCode = 500;
context.Response.StatusDescription = "Internal Server Error";
context.Response.End();
}
} }
/// <summary> /// <summary>

View File

@@ -1,4 +1,6 @@
using System.Linq; using System;
using System.Collections.Generic;
using System.Linq;
using Crestron.SimplSharp.WebScripting; using Crestron.SimplSharp.WebScripting;
using Newtonsoft.Json; using Newtonsoft.Json;
using PepperDash.Core; using PepperDash.Core;
@@ -10,8 +12,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
{ {
private const string Key = "GetTypesRequestHandler"; private const string Key = "GetTypesRequestHandler";
private const uint Trace = 0; private const uint Trace = 0;
private const uint Info = 0; private const uint Info = 1;
private const uint Verbose = 0; private const uint Verbose = 2;
/// <summary> /// <summary>
/// Handles CONNECT method requests /// Handles CONNECT method requests
@@ -41,13 +43,9 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
/// <param name="context"></param> /// <param name="context"></param>
protected override void HandleGet(HttpCwsContext context) protected override void HandleGet(HttpCwsContext context)
{ {
// TODO [ ] DeviceFactory.FactoryMethods dictionary is private and the method GetDeviceFactoryTypes has a return type void var routeData = context.Request.RouteData;
// added new public method to return the DeviceFactory.FactoryMethod dictionary if (routeData == null)
var types = DeviceFactory.GetDeviceFactoryDictionary(null);
if (types == null)
{ {
Debug.Console(Verbose, "Get device factory dictionary failed");
context.Response.StatusCode = 400; context.Response.StatusCode = 400;
context.Response.StatusDescription = "Bad Request"; context.Response.StatusDescription = "Bad Request";
context.Response.End(); context.Response.End();
@@ -55,24 +53,47 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
return; return;
} }
types.OrderBy(t => t.Key); var routeDataJson = JsonConvert.SerializeObject(routeData, Formatting.Indented);
Debug.Console(Verbose, "[{0}] routeData:\n{1}", Key.ToLower(), routeDataJson);
var js = JsonConvert.SerializeObject(types, Formatting.Indented, new JsonSerializerSettings var types = DeviceFactory.GetDeviceFactoryDictionary(string.Empty).Select(type => new
{ {
ReferenceLoopHandling = ReferenceLoopHandling.Ignore, Type = type.Key,
NullValueHandling = NullValueHandling.Ignore, Description = type.Value.Description,
MissingMemberHandling = MissingMemberHandling.Ignore, CType = type.Value.CType == null ? "---" : type.Value.CType.ToString()
DefaultValueHandling = DefaultValueHandling.Ignore, }).Cast<object>().ToList();
TypeNameHandling = TypeNameHandling.None
});
//Debug.Console(Verbose, "[{0}] HandleGet: \x0d\x0a{1}", Key.ToLower(), js);
context.Response.StatusCode = 200; if (types == null)
context.Response.StatusDescription = "OK"; {
context.Response.ContentType = "application/json"; context.Response.StatusCode = 400;
context.Response.ContentEncoding = System.Text.Encoding.UTF8; context.Response.StatusDescription = "Bad Request";
context.Response.Write(js, false); context.Response.End();
context.Response.End();
return;
}
try
{
var js = JsonConvert.SerializeObject(types, Formatting.Indented);
//Debug.Console(Verbose, "[{0}] HandleGet: \x0d\x0a{1}", Key.ToLower(), js);
context.Response.StatusCode = 200;
context.Response.StatusDescription = "OK";
context.Response.ContentType = "application/json";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.Write(js, false);
context.Response.End();
}
catch (Exception ex)
{
Debug.Console(Info, "[{0}] HandleGet Exception Message: {1}", Key.ToLower(), ex.Message);
Debug.Console(Verbose, "[{0}] HandleGet Exception StackTrace: {1}", Key.ToLower(), ex.StackTrace);
if (ex.InnerException != null) Debug.Console(Verbose, "[{0}] HandleGet Exception InnerException: {1}", Key.ToLower(), ex.InnerException);
context.Response.StatusCode = 500;
context.Response.StatusDescription = "Internal Server Error";
context.Response.End();
}
} }
/// <summary> /// <summary>

View File

@@ -9,8 +9,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
{ {
private const string Key = "ReportVersionsRequestHandler"; private const string Key = "ReportVersionsRequestHandler";
private const uint Trace = 0; private const uint Trace = 0;
private const uint Info = 0; private const uint Info = 1;
private const uint Verbose = 0; private const uint Verbose = 2;
/// <summary> /// <summary>
/// Handles CONNECT method requests /// Handles CONNECT method requests

View File

@@ -11,8 +11,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
{ {
private const string Key = "SetDeviceStreamDebugRequestHandler"; private const string Key = "SetDeviceStreamDebugRequestHandler";
private const uint Trace = 0; private const uint Trace = 0;
private const uint Info = 0; private const uint Info = 1;
private const uint Verbose = 0; private const uint Verbose = 2;
/// <summary> /// <summary>
/// Handles CONNECT method requests /// Handles CONNECT method requests

View File

@@ -9,8 +9,8 @@ namespace PepperDash.Essentials.Core.Web.RequestHandlers
{ {
private const string Key = "ShowConfigRequestHandler"; private const string Key = "ShowConfigRequestHandler";
private const uint Trace = 0; private const uint Trace = 0;
private const uint Info = 0; private const uint Info = 1;
private const uint Verbose = 0; private const uint Verbose = 2;
/// <summary> /// <summary>
/// Handles CONNECT method requests /// Handles CONNECT method requests