Send Error Object on SendError (#304)

This change ensures that when calling `requestContext.SendError` you are only able to supply parameters that match the language service beta protocol expected Error object. In other words, you have to provide an error message and optionally and error code.

# **BREAKING API CHANGES**
This will break displaying errors in Microsoft/vscode-mssql. I will be making changes to properly handle the error object shortly.

* Adding contract for returning Error objects as per LanguageService "protocol"

* Fixes throughout codebase to send only error message in error cases
Cleanup of CredentialServiceTest unit test class
Adding standard error handling for event flow validator

* Adding optional data field as per protocol spec
https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md

* Adding optional validation for error objects
This commit is contained in:
Benjamin Russell
2017-04-05 14:47:37 -07:00
committed by GitHub
parent f9138b27df
commit 2eb60f45c9
18 changed files with 260 additions and 233 deletions

View File

@@ -0,0 +1,29 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
namespace Microsoft.SqlTools.Hosting.Contracts
{
/// <summary>
/// Defines the message contract for errors returned via SendError.
/// </summary>
public class Error
{
/// <summary>
/// Error code. If omitted will default to 0
/// </summary>
public int Code { get; set; }
/// <summary>
/// Optional information to return with the error
/// </summary>
public object Data { get; set; }
/// <summary>
/// Error message
/// </summary>
public string Message { get; set; }
}
}

View File

@@ -3,8 +3,10 @@
// 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;
using Newtonsoft.Json.Linq;
namespace Microsoft.SqlTools.Hosting.Protocol
@@ -37,13 +39,26 @@ namespace Microsoft.SqlTools.Hosting.Protocol
eventParams);
}
public virtual async Task SendError(object errorDetails)
public virtual Task SendError(string errorMessage, int errorCode = 0, object data = null)
{
await this.messageWriter.WriteMessage(
// Build the error message
Error error = new Error
{
Message = errorMessage,
Code = errorCode,
Data = data
};
return this.messageWriter.WriteMessage(
Message.ResponseError(
requestMessage.Id,
requestMessage.Method,
JToken.FromObject(errorDetails)));
JToken.FromObject(error)));
}
public virtual Task SendError(Exception e)
{
// Overload to use the parameterized error handler
return SendError(e.Message, e.HResult, e.ToString());
}
}
}