Skip to content

Commit

Permalink
Provide good error when going to invalid URL/path
Browse files Browse the repository at this point in the history
  • Loading branch information
wwwillchen committed Nov 22, 2023
1 parent 4e074ce commit e1b22ba
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions optic/exceptions/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package(
default_visibility = ["//:optic_internal"],
)

py_library(
name = "exceptions",
srcs = glob(["*.py"]),
)
15 changes: 15 additions & 0 deletions optic/exceptions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class OpticException(Exception):
pass


class OpticUserException(Exception):
def __str__(self):
return f"User Error: {super().__str__()}"


class OpticInternalException(Exception):
pass


class OpticDeveloperException(Exception):
pass
1 change: 1 addition & 0 deletions optic/lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ py_library(
name = "lib",
srcs = glob(["*.py"]),
deps = [
"//optic/exceptions",
"//optic/state",
"//protos:ui_py_proto",
],
Expand Down
12 changes: 12 additions & 0 deletions optic/lib/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
from typing import Any, Callable
from .session import Session
from optic.state.state import Store
from optic.exceptions import OpticUserException

Handler = Callable[[Any, Any], None]
newline = "\n"


class Runtime:
Expand All @@ -24,6 +26,16 @@ def reset_session(self):
self._session = Session(Store(deepcopy(self._initial_state), self.get_handler))

def run_path(self, path: str) -> None:
if path not in self._path_fns:
paths = list(self._path_fns.keys())
paths.sort()
raise OpticUserException(
f"""Accessed path: {path} not registered
Try one of the following paths:
{newline.join(paths)}
"""
)
self._path_fns[path]()

def register_path_fn(self, path: str, fn: Callable[[], None]) -> None:
Expand Down
1 change: 1 addition & 0 deletions web/src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { ChannelService, ChannelStatus } from "../services/channel_service";
font-size: 1.5rem;
font-weight: 500;
padding-bottom: 16px;
white-space: preserve;
}
.exception-title {
Expand Down

0 comments on commit e1b22ba

Please sign in to comment.