Move error handling for filesystem to frontend
This commit is contained in:
48
svelte/src/filesystem/ErrorPage.svelte
Normal file
48
svelte/src/filesystem/ErrorPage.svelte
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import TextBlock from "layout/TextBlock.svelte";
|
||||||
|
import { type FSNavigator } from "./FSNavigator";
|
||||||
|
import { onMount } from "svelte";
|
||||||
|
import { breadcrumbs_store } from "wrap/BreadcrumbStore";
|
||||||
|
|
||||||
|
let {
|
||||||
|
nav
|
||||||
|
}: {
|
||||||
|
nav: FSNavigator;
|
||||||
|
} = $props();
|
||||||
|
|
||||||
|
onMount(() => {
|
||||||
|
return nav.subscribe(nav => {
|
||||||
|
breadcrumbs_store.set(breadcrumbs)
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
})
|
||||||
|
</script>
|
||||||
|
|
||||||
|
{#snippet breadcrumbs()}
|
||||||
|
{$nav.navigation_error}
|
||||||
|
{/snippet}
|
||||||
|
|
||||||
|
<TextBlock>
|
||||||
|
{#if $nav.navigation_error === "not_found" || $nav.navigation_error === "path_not_found"}
|
||||||
|
<h1>Page not found</h1>
|
||||||
|
<p>
|
||||||
|
This page could not be found.
|
||||||
|
</p>
|
||||||
|
{:else if $nav.navigation_error === "permission_denied" || $nav.navigation_error === "forbidden"}
|
||||||
|
<h1>Permission denied</h1>
|
||||||
|
<p>
|
||||||
|
You are not allowed to access this resource.
|
||||||
|
</p>
|
||||||
|
{:else if $nav.navigation_error === "unavailable_for_legal_reasons"}
|
||||||
|
<h1>Unavailable for legal reasons</h1>
|
||||||
|
<p>
|
||||||
|
This content has been removed for breaking the law.
|
||||||
|
</p>
|
||||||
|
{:else}
|
||||||
|
<h1>Unknown error code</h1>
|
||||||
|
<p>
|
||||||
|
{$nav.navigation_error}
|
||||||
|
</p>
|
||||||
|
{/if}
|
||||||
|
</TextBlock>
|
||||||
@@ -54,6 +54,7 @@ export class FSNavigator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
last_requested_path: string = ""
|
last_requested_path: string = ""
|
||||||
|
navigation_error: string = ""
|
||||||
navigate = async (path: string, push_history: boolean) => {
|
navigate = async (path: string, push_history: boolean) => {
|
||||||
if (path === this.last_requested_path) {
|
if (path === this.last_requested_path) {
|
||||||
console.debug("FSNavigator: Requested path ", path, " is equal to current path. Debouncing")
|
console.debug("FSNavigator: Requested path ", path, " is equal to current path. Debouncing")
|
||||||
@@ -72,14 +73,14 @@ export class FSNavigator {
|
|||||||
const resp = await fs_get_node(path)
|
const resp = await fs_get_node(path)
|
||||||
this.open_node(resp, push_history)
|
this.open_node(resp, push_history)
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
if (err.value && err.value === "path_not_found") {
|
if (err.value !== undefined && err.value === "path_not_found") {
|
||||||
if (path !== this.path[0].path && path !== "/" && path !== "") {
|
if (path !== this.path[0].path && path !== "/" && path !== "") {
|
||||||
console.debug("Path", path, "was not found, trying to navigate to parent")
|
console.debug("Path", path, "was not found, trying to navigate to parent")
|
||||||
this.navigate(fs_split_path(path).parent, push_history)
|
this.navigate(fs_split_path(path).parent, push_history)
|
||||||
}
|
}
|
||||||
} else if (err.message) {
|
} else if (err.value !== undefined) {
|
||||||
console.error(err)
|
this.navigation_error = err.value
|
||||||
alert("Error: " + err.message)
|
this.notify_subscribers()
|
||||||
} else {
|
} else {
|
||||||
console.error(err)
|
console.error(err)
|
||||||
alert("Error: " + err)
|
alert("Error: " + err)
|
||||||
@@ -133,6 +134,7 @@ export class FSNavigator {
|
|||||||
this.children = node.children
|
this.children = node.children
|
||||||
this.permissions = node.permissions
|
this.permissions = node.permissions
|
||||||
this.context = node.context
|
this.context = node.context
|
||||||
|
this.navigation_error = "" // Clear the error value
|
||||||
this.initialized = true
|
this.initialized = true
|
||||||
|
|
||||||
console.debug("Opened node", node)
|
console.debug("Opened node", node)
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import { css_from_path } from "filesystem/edit_window/Branding";
|
|||||||
import AffiliatePrompt from "user_home/AffiliatePrompt.svelte";
|
import AffiliatePrompt from "user_home/AffiliatePrompt.svelte";
|
||||||
import { current_page_store } from "wrap/RouterStore";
|
import { current_page_store } from "wrap/RouterStore";
|
||||||
import SearchBar from "./SearchBar.svelte";
|
import SearchBar from "./SearchBar.svelte";
|
||||||
|
import ErrorPage from "./ErrorPage.svelte";
|
||||||
|
|
||||||
let file_preview: FilePreview = $state()
|
let file_preview: FilePreview = $state()
|
||||||
let toolbar: Toolbar = $state()
|
let toolbar: Toolbar = $state()
|
||||||
@@ -141,6 +142,7 @@ const keydown = (e: KeyboardEvent) => {
|
|||||||
|
|
||||||
<svelte:window onkeydown={keydown} />
|
<svelte:window onkeydown={keydown} />
|
||||||
|
|
||||||
|
{#if $nav.navigation_error === ""}
|
||||||
<div class="filesystem">
|
<div class="filesystem">
|
||||||
<Breadcrumbs nav={nav} edit_window={edit_window}/>
|
<Breadcrumbs nav={nav} edit_window={edit_window}/>
|
||||||
|
|
||||||
@@ -164,6 +166,10 @@ const keydown = (e: KeyboardEvent) => {
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{:else}
|
||||||
|
<ErrorPage nav={nav} />
|
||||||
|
{/if}
|
||||||
|
|
||||||
<SearchBar bind:this={search_bar} nav={nav}/>
|
<SearchBar bind:this={search_bar} nav={nav}/>
|
||||||
|
|
||||||
<DetailsWindow nav={nav} bind:this={details_window} bind:visible={details_visible} />
|
<DetailsWindow nav={nav} bind:this={details_window} bind:visible={details_visible} />
|
||||||
|
|||||||
@@ -25,22 +25,31 @@ func (wc *WebController) serveDirectory(w http.ResponseWriter, r *http.Request,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
pdapi := wc.api.RealIP(util.RemoteAddress(r)).RealAgent(r.UserAgent())
|
var pdapi = wc.api.RealIP(util.RemoteAddress(r)).RealAgent(r.UserAgent())
|
||||||
|
|
||||||
|
if apikey, err := wc.getAPIKey(r); err == nil {
|
||||||
|
pdapi = pdapi.Login(apikey)
|
||||||
|
}
|
||||||
|
|
||||||
node, err := pdapi.GetFilesystemPath(path)
|
node, err := pdapi.GetFilesystemPath(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if apiErr, ok := errors.AsType[pixelapi.Error](err); ok {
|
if apiErr, ok := errors.AsType[pixelapi.Error](err); ok {
|
||||||
|
// Set the proper response code for the error message
|
||||||
switch apiErr.StatusCode {
|
switch apiErr.StatusCode {
|
||||||
case "not_found", "path_not_found":
|
case "not_found", "path_not_found":
|
||||||
wc.serveNotFound(w, r)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
case "forbidden":
|
case "forbidden", "permission_denied":
|
||||||
wc.serveForbidden(w, r)
|
w.WriteHeader(http.StatusForbidden)
|
||||||
|
case "unavailable_for_legal_reasons":
|
||||||
|
w.WriteHeader(http.StatusUnavailableForLegalReasons)
|
||||||
case "authentication_required":
|
case "authentication_required":
|
||||||
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
||||||
case "unavailable_for_legal_reasons":
|
return
|
||||||
wc.serveUnavailableForLegalReasons(w, r)
|
}
|
||||||
case "permission_denied":
|
|
||||||
wc.serveForbidden(w, r)
|
// Let the JS figure it out
|
||||||
|
if err = wc.templates.Run(w, r, "wrap", td); err != nil {
|
||||||
|
log.Error("Failed to run template: %s", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Error("Failed to get path: %s", err)
|
log.Error("Failed to get path: %s", err)
|
||||||
@@ -57,21 +66,3 @@ func (wc *WebController) serveDirectory(w http.ResponseWriter, r *http.Request,
|
|||||||
log.Error("Error executing template filesystem: %s", err)
|
log.Error("Error executing template filesystem: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wc *WebController) serveForbidden(w http.ResponseWriter, r *http.Request) {
|
|
||||||
log.Debug("Forbidden: %s", r.URL)
|
|
||||||
w.WriteHeader(http.StatusForbidden)
|
|
||||||
if err := wc.templates.Run(w, r, "wrap", wc.newTemplateData(r)); err != nil {
|
|
||||||
log.Error("Failed to run template 403: %s", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (wc *WebController) serveNotFound(w http.ResponseWriter, r *http.Request) {
|
|
||||||
log.Debug("Not Found: %s", r.URL)
|
|
||||||
w.WriteHeader(http.StatusNotFound)
|
|
||||||
wc.templates.Run(w, r, "404", wc.newTemplateData(r))
|
|
||||||
}
|
|
||||||
func (wc *WebController) serveUnavailableForLegalReasons(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusUnavailableForLegalReasons)
|
|
||||||
wc.templates.Run(w, r, "451", wc.newTemplateData(r))
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user