コード例 #1
0
        private void OnCommandFailed(
            CommandFailed commandFailed,
            IJupyterMessageSender jupyterMessageSender)
        {
            var traceBack = new List <string>();

            switch (commandFailed.Exception)
            {
            case CodeSubmissionCompilationErrorException _:
                traceBack.Add(commandFailed.Message);
                break;

            default:
                traceBack.Add("Unhandled Exception");
                traceBack.Add(commandFailed.Message);
                traceBack.AddRange(commandFailed.Exception?.StackTrace?.Split(new[] { Environment.NewLine }, StringSplitOptions.None) ?? Enumerable.Empty <string>());
                break;
            }

            var errorContent = new Error(
                eName: "Unhandled Exception",
                eValue: commandFailed.Message,
                traceback: traceBack
                );

            // send on iopub
            jupyterMessageSender.Send(errorContent);


            //  reply Error
            var executeReplyPayload = new ExecuteReplyError(errorContent, executionCount: _executionCount);

            // send to server
            jupyterMessageSender.Send(executeReplyPayload);
        }
コード例 #2
0
        private void SendErrorToIOPub(Message message, PublisherSocket ioPub, IErrorResult error)
        {
            var executeReply = new ExecuteReplyError()
            {
                ExecutionCount = executionCount,
                EName          = error.Name,
                EValue         = error.Message,
                Traceback      = error.StackTrace
            };
            Message executeReplyMessage = new Message(MessageTypeValues.Error, JsonConvert.SerializeObject(executeReply), message.Header)
            {
                Identifiers = message.Identifiers
            };

            this.messageSender.Send(executeReplyMessage, ioPub);
            this.logger.LogInformation(string.Format("Sending message to IOPub {0}", JsonConvert.SerializeObject(executeReplyMessage)));

            var errorMessage = new StderrMessage()
            {
                Text = error.Message
            };

            Message stderrMessage = new Message(MessageTypeValues.Stream, JsonConvert.SerializeObject(errorMessage), message.Header)
            {
                Identifiers = message.Identifiers
            };

            this.messageSender.Send(stderrMessage, ioPub);
            this.logger.LogInformation(string.Format("Sending message to IOPub {0}", JsonConvert.SerializeObject(stderrMessage)));
        }
コード例 #3
0
        public async Task Handle(JupyterRequestContext context)
        {
            var executeRequest = GetJupyterRequest(context);

            context.RequestHandlerStatus.SetAsBusy();
            var executionCount = executeRequest.Silent ? _executionCount : Interlocked.Increment(ref _executionCount);

            var command   = new SubmitCode(executeRequest.Code, "csharp");
            var id        = Guid.NewGuid();
            var transient = new Dictionary <string, object> {
                { "display_id", id.ToString() }
            };
            var openRequest = new InflightRequest(context, executeRequest, executionCount, transient);

            InFlightRequests[command] = openRequest;

            try
            {
                var kernelResult = await Kernel.SendAsync(command);

                openRequest.AddDisposable(kernelResult.KernelEvents.Subscribe(OnKernelResultEvent));
            }
            catch (Exception e)
            {
                InFlightRequests.TryRemove(command, out _);

                var errorContent = new Error(
                    eName: "Unhandled Exception",
                    eValue: $"{e.Message}"
                    );

                if (!executeRequest.Silent)
                {
                    // send on io
                    var error = Message.Create(
                        errorContent,
                        context.Request.Header);
                    context.IoPubChannel.Send(error);

                    // send on stderr
                    var stdErr = new StdErrStream(errorContent.EValue);
                    var stream = Message.Create(
                        stdErr,
                        context.Request.Header);
                    context.IoPubChannel.Send(stream);
                }

                //  reply Error
                var executeReplyPayload = new ExecuteReplyError(errorContent, executionCount: executionCount);

                // send to server
                var executeReply = Message.CreateResponse(
                    executeReplyPayload,
                    context.Request);

                context.ServerChannel.Send(executeReply);
                context.RequestHandlerStatus.SetAsIdle();
            }
        }
コード例 #4
0
        public async Task Handle(JupyterRequestContext context)
        {
            var executeRequest = GetJupyterRequest(context);

            context.RequestHandlerStatus.SetAsBusy();
            var executionCount = executeRequest.Silent ? _executionCount : Interlocked.Increment(ref _executionCount);

            var command = new SubmitCode(executeRequest.Code, "csharp");

            var openRequest = new InflightRequest(context, executeRequest, executionCount);

            InFlightRequests[command] = openRequest;

            try
            {
                await Kernel.SendAsync(command);
            }
            catch (Exception e)
            {
                InFlightRequests.TryRemove(command, out _);

                var errorContent = new Error(
                    eName: "Unhandled Exception",
                    eValue: $"{e.Message}"
                    );

                if (!executeRequest.Silent)
                {
                    // send on io
                    var error = Message.Create(
                        errorContent,
                        context.Request.Header);
                    context.IoPubChannel.Send(error);

                    // send on stderr
                    var stdErr = new StdErrStream(errorContent.EValue);
                    var stream = Message.Create(
                        stdErr,
                        context.Request.Header);
                    context.IoPubChannel.Send(stream);
                }

                //  reply Error
                var executeReplyPayload = new ExecuteReplyError(errorContent, executionCount: executionCount);

                // send to server
                var executeReply = Message.CreateResponse(
                    executeReplyPayload,
                    context.Request);

                context.ServerChannel.Send(executeReply);
                context.RequestHandlerStatus.SetAsIdle();
            }
        }
コード例 #5
0
ファイル: Kernel.cs プロジェクト: stayyule/icsharp.kernel
        internal void sendError(string error, KernelMessage msg)
        {
            var executeReply = new ExecuteReplyError()
            {
                status          = "error",
                execution_count = executionCount,
                ename           = "generic",
                evalue          = error,
                traceback       = new string[] { }
            };

            sendMessage(shellSocket, msg, "execute_reply", executeReply);
            sendMessage(ioSocket, msg, "stream", new { name = "stderr", data = error });
            logMessage(error);
        }
コード例 #6
0
        public void SendExecuteErrorMessage(Message message, RouterSocket shellSocket)
        {
            var executeReply = new ExecuteReplyError()
            {
                ExecutionCount = this.executionCount
            };

            Message executeReplyMessage = new Message(MessageTypeValues.ExecuteReply, JsonConvert.SerializeObject(executeReply), message.Header);

            // Stick the original identifiers on the message so they'll be sent first
            // Necessary since the shell socket is a ROUTER socket
            executeReplyMessage.Identifiers = message.Identifiers;

            this.logger.LogInformation(string.Format("Sending message to Shell {0}", JsonConvert.SerializeObject(executeReplyMessage)));
            this.messageSender.Send(executeReplyMessage, shellSocket);
        }
コード例 #7
0
ファイル: ExecuteRequestHandler.cs プロジェクト: 0xblack/try
        private void OnCommandFailed(
            CommandFailed commandFailed,
            IJupyterMessageSender jupyterMessageSender)
        {
            var errorContent = new Error(
                eName: "Unhandled Exception",
                eValue: commandFailed.Message
                );


            //  reply Error
            var executeReplyPayload = new ExecuteReplyError(errorContent, executionCount: _executionCount);

            // send to server
            jupyterMessageSender.Send(executeReplyPayload);
        }
コード例 #8
0
        private void OnCommandFailed(
            CommandFailed commandFailed,
            IJupyterMessageSender jupyterMessageSender)
        {
            var traceBack = new List <string>();
            var ename     = "Unhandled exception";
            var emsg      = commandFailed.Message;

            switch (commandFailed.Exception)
            {
            case CodeSubmissionCompilationErrorException _:
                // The diagnostics have already been reported
                ename = "Cell not executed";
                emsg  = "compilation error";
                break;

            case null:

                traceBack.Add(commandFailed.Message);
                break;

            default:
                var exception = commandFailed.Exception;

                traceBack.Add(exception.ToString());

                traceBack.AddRange(
                    exception.StackTrace.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries));

                break;
            }

            var errorContent = new Error(
                eName: ename,
                eValue: emsg,
                traceback: traceBack
                );

            // send on iopub
            jupyterMessageSender.Send(errorContent);

            //  reply Error
            var executeReplyPayload = new ExecuteReplyError(errorContent, executionCount: _executionCount);

            // send to server
            jupyterMessageSender.Send(executeReplyPayload);
        }
コード例 #9
0
        private void OnCommandFailed(
            CommandFailed commandFailed,
            IJupyterMessageSender jupyterMessageSender)
        {
            var traceBack = new List <string>();

            switch (commandFailed.Exception)
            {
            case CodeSubmissionCompilationErrorException _:
                traceBack.Add(commandFailed.Message);
                break;

            case null:

                traceBack.Add(commandFailed.Message);
                break;

            default:
                var exception = commandFailed.Exception;

                traceBack.Add(
                    $"{exception.GetType().FullName}: {exception.Message}");

                traceBack.AddRange(
                    exception.StackTrace.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries));

                break;
            }

            var errorContent = new Error(
                eName: "Unhandled exception",
                eValue: commandFailed.Message,
                traceback: traceBack
                );

            // send on iopub
            jupyterMessageSender.Send(errorContent);


            //  reply Error
            var executeReplyPayload = new ExecuteReplyError(errorContent, executionCount: _executionCount);

            // send to server
            jupyterMessageSender.Send(executeReplyPayload);
        }
コード例 #10
0
        private void SendErrorToIOPub(Message message, PublisherSocket ioPub, Exception ex)
        {
            var error = new ExecuteReplyError()
            {
                EName          = ex.GetType().FullName,
                EValue         = ex.Message,
                Traceback      = ex.StackTrace.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).ToList(),
                ExecutionCount = this.executionCount,
                Status         = "error"
            };

            Message executeReplyMessage = new Message(MessageTypeValues.ExecuteReply, JsonConvert.SerializeObject(error), message.Header);

            // Stick the original identifiers on the message so they'll be sent first
            // Necessary since the shell socket is a ROUTER socket
            executeReplyMessage.Identifiers = message.Identifiers;

            this.logger.LogInformation(string.Format("Sending message to IOPub {0}", JsonConvert.SerializeObject(executeReplyMessage)));
            this.messageSender.Send(executeReplyMessage, ioPub);
        }
コード例 #11
0
        private void OnCommandFailed(CommandFailed commandFailed)
        {
            if (!InFlightRequests.TryRemove(commandFailed.GetRootCommand(), out var openRequest))
            {
                return;
            }

            var errorContent = new Error(
                eName: "Unhandled Exception",
                eValue: commandFailed.Message
                );

            if (!openRequest.Request.Silent)
            {
                // send on io
                var error = Message.Create(
                    errorContent,
                    openRequest.Context.Request.Header);
                openRequest.Context.IoPubChannel.Send(error);

                // send on stderr
                var stdErr = new StdErrStream(errorContent.EValue);
                var stream = Message.Create(
                    stdErr,
                    openRequest.Context.Request.Header);
                openRequest.Context.IoPubChannel.Send(stream);
            }

            //  reply Error
            var executeReplyPayload = new ExecuteReplyError(errorContent, executionCount: openRequest.ExecutionCount);

            // send to server
            var executeReply = Message.CreateResponse(
                executeReplyPayload,
                openRequest.Context.Request);

            openRequest.Context.ServerChannel.Send(executeReply);

            openRequest.Context.RequestHandlerStatus.SetAsIdle();
            openRequest.Dispose();
        }
コード例 #12
0
ファイル: ExecuteRequestHandler.cs プロジェクト: eerhardt/try
        private void OnCommandFailed(
            CommandFailed commandFailed,
            Message request,
            IMessageSender serverChannel,
            IMessageSender ioPubChannel)
        {
            var errorContent = new Error(
                eName: "Unhandled Exception",
                eValue: commandFailed.Message
                );

            var isSilent = ((ExecuteRequest)request.Content).Silent;

            if (!isSilent)
            {
                // send on io
                var error = Message.Create(
                    errorContent,
                    request.Header);

                ioPubChannel.Send(error);

                // send on stderr
                var stdErr = Stream.StdErr(errorContent.EValue);
                var stream = Message.Create(
                    stdErr,
                    request.Header);

                ioPubChannel.Send(stream);
            }

            //  reply Error
            var executeReplyPayload = new ExecuteReplyError(errorContent, executionCount: _executionCount);

            // send to server
            var executeReply = Message.CreateResponse(
                executeReplyPayload,
                request);

            serverChannel.Send(executeReply);
        }
コード例 #13
0
        private static void OnCodeSubmissionEvaluatedFailed(CodeSubmissionEvaluationFailed codeSubmissionEvaluationFailed, ConcurrentDictionary <IKernelCommand, InflightRequest> openRequests)
        {
            openRequests.TryRemove(codeSubmissionEvaluationFailed.Command, out var openRequest);

            var errorContent = new Error(
                eName: "Unhandled Exception",
                eValue: $"{codeSubmissionEvaluationFailed.Message}"
                );

            if (!openRequest.Request.Silent)
            {
                // send on io
                var error = Message.Create(
                    errorContent,
                    openRequest.Context.Request.Header);
                openRequest.Context.IoPubChannel.Send(error);

                // send on stderr
                var stdErr = new StdErrStream(errorContent.EValue);
                var stream = Message.Create(
                    stdErr,
                    openRequest.Context.Request.Header);
                openRequest.Context.IoPubChannel.Send(stream);
            }

            //  reply Error
            var executeReplyPayload = new ExecuteReplyError(errorContent, executionCount: openRequest.ExecutionCount);

            // send to server
            var executeReply = Message.CreateResponse(
                executeReplyPayload,
                openRequest.Context.Request);

            openRequest.Context.ServerChannel.Send(executeReply);

            openRequest.Context.RequestHandlerStatus.SetAsIdle();
            openRequest.Dispose();
        }
コード例 #14
0
        public async Task <ICommandDeliveryResult> Handle(
            ICommandDelivery <JupyterRequestContext> delivery)
        {
            switch (delivery.Command.Request.Header.MessageType)
            {
            case MessageTypeValues.ExecuteRequest:
                var transient = new Dictionary <string, object> {
                    { "display_id", Guid.NewGuid().ToString() }
                };

                var jObject        = (JObject)delivery.Command.Request.Content;
                var executeRequest = jObject.ToObject <ExecuteRequest>();

                var code = executeRequest.Code;

                var workspace = new Workspace(
                    files: new[]
                {
                    new File("Program.cs", Scaffold())
                },
                    buffers: new[]
                {
                    new Buffer(new BufferId("Program.cs", "main"), code),
                },
                    workspaceType: "console");

                var workspaceRequest = new WorkspaceRequest(workspace);

                var server = new WorkspaceServerMultiplexer(new PackageRegistry());

                var result = await server.Run(workspaceRequest);

                var messageBuilder = delivery.Command.Builder;
                var ioPubChannel   = delivery.Command.IoPubChannel;
                var serverChannel  = delivery.Command.ServerChannel;

                if (!executeRequest.Silent)
                {
                    _executionCount++;

                    var executeInput = messageBuilder.CreateMessage(
                        MessageTypeValues.ExecuteInput,
                        new ExecuteInput
                    {
                        Code           = code,
                        ExecutionCount = _executionCount
                    },
                        delivery.Command.Request.Header);

                    ioPubChannel.Send(executeInput);
                }

                // execute result
                var output = string.Join("\n", result.Output);


                // executeResult data
                var executeResultData = new ExecuteResult()
                {
                    Data = new JObject
                    {
                        { "text/html", output },
                        { "text/plain", output }
                    },
                    Transient      = transient,
                    ExecutionCount = _executionCount
                };



                var resultSucceeded = result.Succeeded &&
                                      result.Exception == null;

                if (resultSucceeded)
                {
                    // reply ok
                    var executeReplyPayload = new ExecuteReplyOk
                    {
                        ExecutionCount = _executionCount
                    };

                    // send to server
                    var executeReply = messageBuilder.CreateMessage(
                        MessageTypeValues.ExecuteReply,
                        executeReplyPayload,
                        delivery.Command.Request.Header);

                    executeReply.Identifiers = delivery.Command.Request.Identifiers;

                    serverChannel.Send(executeReply);
                }
                else
                {
                    var errorContent = new Error
                    {
                        EName = string.IsNullOrWhiteSpace(result.Exception)
                                        ? "Compile Error"
                                        : "Unhandled Exception",
                        EValue    = output,
                        Traceback = new List <string>()
                    };

                    //  reply Error
                    var executeReplyPayload = new ExecuteReplyError(errorContent)
                    {
                        ExecutionCount = _executionCount
                    };

                    // send to server
                    var executeReply = messageBuilder.CreateMessage(
                        MessageTypeValues.ExecuteReply,
                        executeReplyPayload,
                        delivery.Command.Request.Header);

                    executeReply.Identifiers = delivery.Command.Request.Identifiers;

                    serverChannel.Send(executeReply);

                    if (!executeRequest.Silent)
                    {
                        // send on io
                        var error = messageBuilder.CreateMessage(
                            MessageTypeValues.Error,
                            errorContent,
                            delivery.Command.Request.Header);
                        ioPubChannel.Send(error);

                        // send on stderr
                        var stdErr = new StdErrStream
                        {
                            Text = errorContent.EValue
                        };
                        var stream = messageBuilder.CreateMessage(
                            MessageTypeValues.Stream,
                            stdErr,
                            delivery.Command.Request.Header);
                        ioPubChannel.Send(stream);
                    }
                }

                if (!executeRequest.Silent && resultSucceeded)
                {
                    // send on io
                    var executeResultMessage = messageBuilder.CreateMessage(
                        MessageTypeValues.ExecuteResult,
                        executeResultData,
                        delivery.Command.Request.Header);
                    ioPubChannel.Send(executeResultMessage);
                }

                break;
            }

            return(delivery.Complete());
        }
コード例 #15
0
        private async Task HandleExecuteRequest(ICommandDelivery <JupyterRequestContext> delivery)
        {
            var ioPubChannel  = delivery.Command.IoPubChannel;
            var serverChannel = delivery.Command.ServerChannel;

            var transient = new Dictionary <string, object> {
                { "display_id", Guid.NewGuid().ToString() }
            };

            var executeRequest = delivery.Command.GetRequestContent <ExecuteRequest>();

            var code = executeRequest.Code;

            var workspace = CreateScaffoldWorkspace(code);

            var workspaceRequest = new WorkspaceRequest(workspace);

            var result = await _server.Run(workspaceRequest);

            if (!executeRequest.Silent)
            {
                _executionCount++;

                var executeInput = Message.Create(
                    new ExecuteInput(code: code, executionCount: _executionCount),
                    delivery.Command.Request.Header);

                ioPubChannel.Send(executeInput);
            }

            // execute result
            var output = string.Join("\n", result.Output);


            // executeResult data
            var executeResultData = new ExecuteResult(
                _executionCount,
                transient: transient,
                data: new Dictionary <string, object> {
                { "text/html", output },
                { "text/plain", output }
            });


            var resultSucceeded = result.Succeeded &&
                                  result.Exception == null;

            if (resultSucceeded)
            {
                // reply ok
                var executeReplyPayload = new ExecuteReplyOk(executionCount: _executionCount);


                // send to server
                var executeReply = Message.CreateResponse(
                    executeReplyPayload,
                    delivery.Command.Request);

                serverChannel.Send(executeReply);
            }
            else
            {
                var errorContent = new Error(
                    eName: string.IsNullOrWhiteSpace(result.Exception) ? "Compile Error" : "Unhandled Exception",
                    eValue: output
                    );

                //  reply Error
                var executeReplyPayload = new ExecuteReplyError(errorContent, executionCount: _executionCount);

                // send to server
                var executeReply = Message.CreateResponse(
                    executeReplyPayload,
                    delivery.Command.Request);

                serverChannel.Send(executeReply);

                if (!executeRequest.Silent)
                {
                    // send on io
                    var error = Message.Create(
                        errorContent,
                        delivery.Command.Request.Header);
                    ioPubChannel.Send(error);

                    // send on stderr
                    var stdErr = new StdErrStream(errorContent.EValue);
                    var stream = Message.Create(
                        stdErr,
                        delivery.Command.Request.Header);
                    ioPubChannel.Send(stream);
                }
            }

            if (!executeRequest.Silent && resultSucceeded)
            {
                // send on io
                var executeResultMessage = Message.Create(
                    executeResultData,
                    delivery.Command.Request.Header);
                ioPubChannel.Send(executeResultMessage);
            }
        }