Error handling
Errors are modeled as message descriptions. Each error type is listed in Services.MessageDescription table (also ServiceMessage.Instance). There is human-readable template text and machine-readable identifier number. Error messages are easily logged, stored, indexed, serialized, transmitted, and converted into other formats, such as .json.
IMessageDescription describes error type.
// Choose message description
IMessageDescription messageDescription = Services.MessageDescription.BadQueryNoValue;
// Write description
Console.Error.WriteLine(messageDescription);
// MessageDescription(Code=A0AAC059, Key=Avalanche.Service.BadQueryNoValue, HResult=A0AAC059, Severity=, Template="{request}: No value.", "No value")
IMessage is attached to thrown Exceptions (message).
// Create handler
Delegate decorator = (IQuery<string, string> q) => q.Response.SetValue(q.Request + "-Suffix");
// Create service
IService service = Services.Create(decorator);
try
{
// Issue request that is not handled
service.GetRequired<int, int>(5);
}
// Get e.Data[MessageExceptionExtensions.Key] as IMessage
catch (Exception e) when (e.TryGetAttachedMessage(out IMessage message))
{
// Print message
string print = message.Print();
// Write event with arguments
Console.Error.WriteLine(print);
// Let it fly
throw;
}
.HResult contains identifier of the occured error. HResult number is allocated under the bit mask for third party codes.
// Create handler
Delegate decorator = (IQuery<string, string> q) => q.Response.SetValue(q.Request + "-Suffix");
// Create service
IService service = Services.Create(decorator);
try
{
// Issue request that is not handled
service.GetRequired<int, int>(5);
}
catch (Exception e) when (e.HResult == Services.MessageDescription.BadQueryNoValue.HResult)
{
Console.Error.WriteLine("Query was not handled.");
}
QueryLogger is a handler that prints query results into a ILogger.
Full Example
Full example
using System;
using Avalanche.Service;
using Avalanche.Utilities;
using Avalanche.Message;
using Avalanche.Template;
public class articles_errorhandling
{
public static void Run()
{
{
// <01>
// Choose message description
IMessageDescription messageDescription = Services.MessageDescription.BadQueryNoValue;
// Write description
Console.Error.WriteLine(messageDescription);
// MessageDescription(Code=A0AAC059, Key=Avalanche.Service.BadQueryNoValue, HResult=A0AAC059, Severity=, Template="{request}: No value.", "No value")
// </01>
}
{
try
{
// <02>
// Create handler
Delegate decorator = (IQuery<string, string> q) => q.Response.SetValue(q.Request + "-Suffix");
// Create service
IService service = Services.Create(decorator);
try
{
// Issue request that is not handled
service.GetRequired<int, int>(5);
}
// Get e.Data[MessageExceptionExtensions.Key] as IMessage
catch (Exception e) when (e.TryGetAttachedMessage(out IMessage message))
{
// Print message
string print = message.Print();
// Write event with arguments
Console.Error.WriteLine(print);
// Let it fly
throw;
}
// </02>
}
catch (Exception) { }
try
{
// <03>
// Create handler
Delegate decorator = (IQuery<string, string> q) => q.Response.SetValue(q.Request + "-Suffix");
// Create service
IService service = Services.Create(decorator);
try
{
// Issue request that is not handled
service.GetRequired<int, int>(5);
}
catch (Exception e) when (e.HResult == Services.MessageDescription.BadQueryNoValue.HResult)
{
Console.Error.WriteLine("Query was not handled.");
}
// </03>
} catch (Exception) { }
}
}
}