diff --git a/rpc.go b/rpc.go new file mode 100644 index 0000000..708a243 --- /dev/null +++ b/rpc.go @@ -0,0 +1,51 @@ +package protocol + +// Range of the LSP code space +const ( + LspReservedErrorRangeStart = -32899 + LspReservedErrorRangeEnd = -32800 +) + +// LSP Specific error codes used with JSON RPC. +const ( + // Error code indicating that a server received a notification or + // request before the server received the `initialize` request. + // + // For backwards compatibility these codes are not in the LSP range. + RPCServerNotInitialized int64 = -32002 + RPCUnknownErrorCode int64 = -32001 + + // A request failed but it was syntactically correct, e.g the + // method name was known and the parameters were valid. The error + // message should contain human readable information about why + // the request failed. + // + // @since 3.17.0 + RPCRequestFailed int64 = -32803 + + // The server detected that the content of a document got + // modified outside normal conditions. A server should + // NOT send this error code if it detects a content change + // in its unprocessed messages. The result even computed + // on an older state might still be useful for the client. + // + // If a client decides that a result is not of any use anymore + // the client should cancel the request. + RPCContentModified int64 = -32801 + + // The client has canceled a request and a server has detected + // the cancel. + RPCRequestCancelled int64 = -32800 +) + +func IsLspRPCErrorCode(code int64) bool { + if code >= LspReservedErrorRangeStart && code <= LspReservedErrorRangeEnd { + return true + } + + if code == RPCServerNotInitialized || code == RPCUnknownErrorCode { + return true + } + + return false +} diff --git a/rpc_test.go b/rpc_test.go new file mode 100644 index 0000000..f827aef --- /dev/null +++ b/rpc_test.go @@ -0,0 +1,38 @@ +package protocol_test + +import ( + "testing" + + "github.com/laravel-ls/protocol" +) + +func Test_IsLspRPCErrorCode(t *testing.T) { + if protocol.IsLspRPCErrorCode(5000) { + t.Errorf("5000 is not a valid code, but function returned true") + } + + if protocol.IsLspRPCErrorCode(-9000) { + t.Errorf("-9000 is not a valid code, but function returned true") + } + + if protocol.IsLspRPCErrorCode(-32899) == false { + t.Errorf("-32899 is a valid code, but function returned false") + } + + if protocol.IsLspRPCErrorCode(-32840) == false { + t.Errorf("-32840 is a valid code, but function returned false") + } + + if protocol.IsLspRPCErrorCode(-32800) == false { + t.Errorf("-32800 is a valid code, but function returned false") + } + + // Exception + if protocol.IsLspRPCErrorCode(-32001) == false { + t.Errorf("-32001 is a valid code, but function returned false") + } + + if protocol.IsLspRPCErrorCode(-32002) == false { + t.Errorf("-32002 is a valid code, but function returned false") + } +}