Skip to content

Commit

Permalink
feat: handling exceptions when running pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
WinPlay02 committed Nov 16, 2023
1 parent f13f6c4 commit c453627
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
18 changes: 16 additions & 2 deletions src/safeds_runner/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import json
import logging
import typing
from typing import Any, Optional

import stack_data
from flask import Flask, request
from flask_cors import CORS
from flask_sock import Sock
Expand Down Expand Up @@ -49,7 +51,6 @@ def post_run_program():
@sock.route("/WSRunProgram")
def ws_run_program(ws):
logging.debug(f"Request to WSRunProgram")
send_message(ws, "test", "test")
while True:

Check warning on line 54 in src/safeds_runner/server/main.py

View check run for this annotation

Codecov / codecov/patch

src/safeds_runner/server/main.py#L53-L54

Added lines #L53 - L54 were not covered by tests
# This would be a JSON message
received_message: str = ws.receive()
Expand Down Expand Up @@ -83,7 +84,19 @@ def ws_run_program(ws):
# TODO forward memoization map here
context_globals = {"connection": ws, "send_value": send_value}

Check warning on line 85 in src/safeds_runner/server/main.py

View check run for this annotation

Codecov / codecov/patch

src/safeds_runner/server/main.py#L85

Added line #L85 was not covered by tests
# This should only be called from the extension as it is a security risk
execute_pipeline(code, main['package'], main['module'], main['pipeline'], context_globals)
try:
execute_pipeline(code, main['package'], main['module'], main['pipeline'], context_globals)
send_message(ws, "progress", "done")
except BaseException as error:
send_message(ws, "runtime_error",

Check warning on line 91 in src/safeds_runner/server/main.py

View check run for this annotation

Codecov / codecov/patch

src/safeds_runner/server/main.py#L87-L91

Added lines #L87 - L91 were not covered by tests
{"message": error.__str__(), "backtrace": get_backtrace_info(error)})


def get_backtrace_info(error: BaseException) -> list[dict[str, typing.Any]]:
backtrace_list = []
for frame in stack_data.core.FrameInfo.stack_data(error.__traceback__):
backtrace_list.append({"file": frame.filename, "line": str(frame.lineno)})
return backtrace_list

Check warning on line 99 in src/safeds_runner/server/main.py

View check run for this annotation

Codecov / codecov/patch

src/safeds_runner/server/main.py#L96-L99

Added lines #L96 - L99 were not covered by tests


def send_value(connection, name: str, var_type: str, value: str):
Expand Down Expand Up @@ -126,6 +139,7 @@ def validate_message(message: dict[str, Any]) -> (bool, Optional[str]):
# Allow prints to be unbuffered by default
import functools
import builtins

Check warning on line 141 in src/safeds_runner/server/main.py

View check run for this annotation

Codecov / codecov/patch

src/safeds_runner/server/main.py#L140-L141

Added lines #L140 - L141 were not covered by tests

builtins.print = functools.partial(print, flush=True)

Check warning on line 143 in src/safeds_runner/server/main.py

View check run for this annotation

Codecov / codecov/patch

src/safeds_runner/server/main.py#L143

Added line #L143 was not covered by tests

logging.getLogger().setLevel(logging.DEBUG)
Expand Down
11 changes: 9 additions & 2 deletions src/safeds_runner/server/module_manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import importlib.abc
import typing
from abc import ABC
from importlib.machinery import ModuleSpec
import sys
Expand All @@ -7,6 +8,8 @@
import runpy
import logging

import stack_data


class InMemoryLoader(importlib.abc.SourceLoader, ABC):
def __init__(self, code_bytes: bytes, filename: str):
Expand Down Expand Up @@ -75,8 +78,12 @@ def _execute_pipeline(code: dict[str, dict[str, str]], sdspackage: str, sdsmodul
pipeline_finder = InMemoryFinder(code)
pipeline_finder.attach()
main_module = f"gen_{sdsmodule}_{sdspipeline}"
runpy.run_module(main_module, run_name="__main__") # TODO Is the Safe-DS-Package relevant here?
pipeline_finder.detach()
try:
runpy.run_module(main_module, run_name="__main__") # TODO Is the Safe-DS-Package relevant here?
except BaseException:
raise # This should keep the backtrace

Check warning on line 84 in src/safeds_runner/server/module_manager.py

View check run for this annotation

Codecov / codecov/patch

src/safeds_runner/server/module_manager.py#L83-L84

Added lines #L83 - L84 were not covered by tests
finally:
pipeline_finder.detach()


def execute_pipeline(code: dict[str, dict[str, str]], sdspackage: str, sdsmodule: str, sdspipeline: str,
Expand Down

0 comments on commit c453627

Please sign in to comment.