Files
sqltoolsservice/src/Microsoft.SqlTools.Hosting/Hosting/Protocol/RequestContext.cs
2022-03-04 16:38:01 -08:00

63 lines
1.8 KiB
C#

//
// 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.Contracts;
using Error = Microsoft.SqlTools.Hosting.Contracts.Error;
namespace Microsoft.SqlTools.Hosting.Protocol
{
public class RequestContext<TResult> : IEventSender
{
private readonly Message requestMessage;
private readonly MessageWriter messageWriter;
public RequestContext(Message requestMessage, MessageWriter messageWriter)
{
this.requestMessage = requestMessage;
this.messageWriter = messageWriter;
}
public RequestContext() { }
public virtual async Task SendResult(TResult resultDetails)
{
await this.messageWriter.WriteResponse(
resultDetails,
requestMessage.Method,
requestMessage.Id);
}
public virtual async Task SendEvent<TParams>(EventType<TParams> eventType, TParams eventParams)
{
await this.messageWriter.WriteEvent(
eventType,
eventParams);
}
public virtual Task SendError(string errorMessage, int errorCode = 0)
{
// Build the error message
Error error = new Error
{
Message = errorMessage,
Code = errorCode
};
return this.messageWriter.WriteError(
requestMessage.Method,
requestMessage.Id,
error);
}
public virtual Task SendError(Exception e)
{
// Overload to use the parameterized error handler
return SendError(e.Message, e.HResult);
}
}
}