mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-01-14 01:25:40 -05:00
Shifting handler-running methods to shared location (#1851)
* Shifting helper methods to shared location * widening scope
This commit is contained in:
@@ -18,7 +18,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SqlProjects
|
||||
/// <summary>
|
||||
/// Main class for SqlProjects service
|
||||
/// </summary>
|
||||
class SqlProjectsService
|
||||
public sealed class SqlProjectsService : BaseService
|
||||
{
|
||||
private static readonly Lazy<SqlProjectsService> instance = new Lazy<SqlProjectsService>(() => new SqlProjectsService());
|
||||
|
||||
@@ -216,74 +216,6 @@ namespace Microsoft.SqlTools.ServiceLayer.SqlProjects
|
||||
|
||||
#region Helper methods
|
||||
|
||||
/// <summary>
|
||||
/// Synchronous action with standard ResultStatus
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
/// <param name="requestContext"></param>
|
||||
/// <returns></returns>
|
||||
private async Task RunWithErrorHandling(Action action, RequestContext<ResultStatus> requestContext)
|
||||
{
|
||||
await RunWithErrorHandling(async () => await Task.Run(action), requestContext);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronous action with standard ResultStatus
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
/// <param name="requestContext"></param>
|
||||
/// <returns></returns>
|
||||
private async Task RunWithErrorHandling(Func<Task> action, RequestContext<ResultStatus> requestContext)
|
||||
{
|
||||
await RunWithErrorHandling<ResultStatus>(async () =>
|
||||
{
|
||||
await action();
|
||||
|
||||
return new ResultStatus()
|
||||
{
|
||||
Success = true,
|
||||
ErrorMessage = null
|
||||
};
|
||||
}, requestContext);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Synchronous action with custom result
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="action"></param>
|
||||
/// <param name="requestContext"></param>
|
||||
/// <returns></returns>
|
||||
private async Task RunWithErrorHandling<T>(Func<T> action, RequestContext<T> requestContext) where T : ResultStatus, new()
|
||||
{
|
||||
await RunWithErrorHandling<T>(async () => await Task.Run(action), requestContext);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronous action with custom result
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="action"></param>
|
||||
/// <param name="requestContext"></param>
|
||||
/// <returns></returns>
|
||||
private async Task RunWithErrorHandling<T>(Func<Task<T>> action, RequestContext<T> requestContext) where T : ResultStatus, new()
|
||||
{
|
||||
try
|
||||
{
|
||||
T result = await action();
|
||||
await requestContext.SendResult(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await requestContext.SendResult(new T()
|
||||
{
|
||||
Success = false,
|
||||
ErrorMessage = ex.Message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private SqlProject GetProject(string projectUri)
|
||||
{
|
||||
if (!Projects.ContainsKey(projectUri))
|
||||
|
||||
89
src/Microsoft.SqlTools.ServiceLayer/Utility/BaseService.cs
Normal file
89
src/Microsoft.SqlTools.ServiceLayer/Utility/BaseService.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
//
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
||||
//
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SqlTools.Hosting.Protocol;
|
||||
|
||||
namespace Microsoft.SqlTools.ServiceLayer.Utility
|
||||
{
|
||||
/// <summary>
|
||||
/// Base class for services that contains helpful methods
|
||||
/// </summary>
|
||||
public abstract class BaseService
|
||||
{
|
||||
#region Runners with error handling
|
||||
|
||||
/// <summary>
|
||||
/// Synchronous action with standard ResultStatus
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
/// <param name="requestContext"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task RunWithErrorHandling(Action action, RequestContext<ResultStatus> requestContext)
|
||||
{
|
||||
await RunWithErrorHandling(async () => await Task.Run(action), requestContext);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronous action with standard ResultStatus
|
||||
/// </summary>
|
||||
/// <param name="action"></param>
|
||||
/// <param name="requestContext"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task RunWithErrorHandling(Func<Task> action, RequestContext<ResultStatus> requestContext)
|
||||
{
|
||||
await RunWithErrorHandling<ResultStatus>(async () =>
|
||||
{
|
||||
await action();
|
||||
|
||||
return new ResultStatus()
|
||||
{
|
||||
Success = true,
|
||||
ErrorMessage = null
|
||||
};
|
||||
}, requestContext);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Synchronous action with custom result
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="action"></param>
|
||||
/// <param name="requestContext"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task RunWithErrorHandling<T>(Func<T> action, RequestContext<T> requestContext) where T : ResultStatus, new()
|
||||
{
|
||||
await RunWithErrorHandling<T>(async () => await Task.Run(action), requestContext);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronous action with custom result
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="action"></param>
|
||||
/// <param name="requestContext"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task RunWithErrorHandling<T>(Func<Task<T>> action, RequestContext<T> requestContext) where T : ResultStatus, new()
|
||||
{
|
||||
try
|
||||
{
|
||||
T result = await action();
|
||||
await requestContext.SendResult(result);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await requestContext.SendResult(new T()
|
||||
{
|
||||
Success = false,
|
||||
ErrorMessage = ex.Message
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user