mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2025-11-14 00:31:33 +10:00
Drop will no longer crash when the server goes down
This commit is contained in:
66
pages/error.vue
Normal file
66
pages/error.vue
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
<template>
|
||||||
|
<div
|
||||||
|
class="grid min-h-full grid-cols-1 grid-rows-[1fr,auto,1fr] lg:grid-cols-[max(50%,36rem),1fr]"
|
||||||
|
>
|
||||||
|
<header
|
||||||
|
class="mx-auto w-full max-w-7xl px-6 pt-6 sm:pt-10 lg:col-span-2 lg:col-start-1 lg:row-start-1 lg:px-8"
|
||||||
|
>
|
||||||
|
<Logo class="h-10 w-auto sm:h-12" />
|
||||||
|
</header>
|
||||||
|
<main
|
||||||
|
class="mx-auto w-full max-w-7xl px-6 py-24 sm:py-32 lg:col-span-2 lg:col-start-1 lg:row-start-2 lg:px-8"
|
||||||
|
>
|
||||||
|
<div class="max-w-lg">
|
||||||
|
<h1
|
||||||
|
class="mt-4 text-3xl font-bold font-display tracking-tight text-zinc-100 sm:text-5xl"
|
||||||
|
>
|
||||||
|
Unrecoverable error
|
||||||
|
</h1>
|
||||||
|
<p class="mt-6 text-base leading-7 text-zinc-400">
|
||||||
|
Drop encountered an error that it couldn't handle. Please restart the
|
||||||
|
application and file a bug report.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</main>
|
||||||
|
<footer class="self-end lg:col-span-2 lg:col-start-1 lg:row-start-3">
|
||||||
|
<div class="border-t border-blue-600 bg-zinc-900 py-10">
|
||||||
|
<nav
|
||||||
|
class="mx-auto flex w-full max-w-7xl items-center gap-x-4 px-6 text-sm leading-7 text-zinc-400 lg:px-8"
|
||||||
|
>
|
||||||
|
<a href="#">Documentation</a>
|
||||||
|
<svg
|
||||||
|
viewBox="0 0 2 2"
|
||||||
|
aria-hidden="true"
|
||||||
|
class="h-0.5 w-0.5 fill-zinc-700"
|
||||||
|
>
|
||||||
|
<circle cx="1" cy="1" r="1" />
|
||||||
|
</svg>
|
||||||
|
<a href="#">Troubleshooting</a>
|
||||||
|
<svg
|
||||||
|
viewBox="0 0 2 2"
|
||||||
|
aria-hidden="true"
|
||||||
|
class="h-0.5 w-0.5 fill-zinc-700"
|
||||||
|
>
|
||||||
|
<circle cx="1" cy="1" r="1" />
|
||||||
|
</svg>
|
||||||
|
<NuxtLink to="/setup/server">Switch instance</NuxtLink>
|
||||||
|
</nav>
|
||||||
|
</div>
|
||||||
|
</footer>
|
||||||
|
<div
|
||||||
|
class="hidden lg:relative lg:col-start-2 lg:row-start-1 lg:row-end-4 lg:block"
|
||||||
|
>
|
||||||
|
<img
|
||||||
|
src="@/assets/wallpaper.jpg"
|
||||||
|
alt=""
|
||||||
|
class="absolute inset-0 h-full w-full object-cover"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
definePageMeta({
|
||||||
|
layout: "mini",
|
||||||
|
});
|
||||||
|
</script>
|
||||||
11
plugins/global-error-handler.ts
Normal file
11
plugins/global-error-handler.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
export default defineNuxtPlugin((nuxtApp) => {
|
||||||
|
nuxtApp.vueApp.config.errorHandler = (error, instance, info) => {
|
||||||
|
// handle error, e.g. report to a service
|
||||||
|
};
|
||||||
|
|
||||||
|
// Also possible
|
||||||
|
nuxtApp.hook("vue:error", (error, instance, info) => {
|
||||||
|
const router = useRouter();
|
||||||
|
router.replace("/error");
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -13,6 +13,7 @@ use url::{ParseError, Url};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::{DatabaseAuth, DatabaseImpls},
|
db::{DatabaseAuth, DatabaseImpls},
|
||||||
|
remote::RemoteAccessError,
|
||||||
AppState, AppStatus, User, DB,
|
AppState, AppStatus, User, DB,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -68,43 +69,6 @@ pub fn generate_authorization_header() -> String {
|
|||||||
format!("Nonce {} {} {}", certs.client_id, nonce, signature)
|
format!("Nonce {} {} {}", certs.client_id, nonce, signature)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum RemoteAccessError {
|
|
||||||
FetchError(reqwest::Error),
|
|
||||||
ParsingError(ParseError),
|
|
||||||
GenericErrror(String),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for RemoteAccessError {
|
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
RemoteAccessError::FetchError(error) => write!(f, "{}", error),
|
|
||||||
RemoteAccessError::GenericErrror(error) => write!(f, "{}", error),
|
|
||||||
RemoteAccessError::ParsingError(parse_error) => {
|
|
||||||
write!(f, "{}", parse_error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<reqwest::Error> for RemoteAccessError {
|
|
||||||
fn from(err: reqwest::Error) -> Self {
|
|
||||||
RemoteAccessError::FetchError(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl From<String> for RemoteAccessError {
|
|
||||||
fn from(err: String) -> Self {
|
|
||||||
RemoteAccessError::GenericErrror(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
impl From<ParseError> for RemoteAccessError {
|
|
||||||
fn from(err: ParseError) -> Self {
|
|
||||||
RemoteAccessError::ParsingError(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::error::Error for RemoteAccessError {}
|
|
||||||
|
|
||||||
pub fn fetch_user() -> Result<User, RemoteAccessError> {
|
pub fn fetch_user() -> Result<User, RemoteAccessError> {
|
||||||
let base_url = DB.fetch_base_url();
|
let base_url = DB.fetch_base_url();
|
||||||
|
|
||||||
@ -118,7 +82,7 @@ pub fn fetch_user() -> Result<User, RemoteAccessError> {
|
|||||||
.send()?;
|
.send()?;
|
||||||
|
|
||||||
if response.status() != 200 {
|
if response.status() != 200 {
|
||||||
return Err(format!("Failed to fetch user: {}", response.status()).into());
|
return Err(response.status().as_u16().into());
|
||||||
}
|
}
|
||||||
|
|
||||||
let user = response.json::<User>()?;
|
let user = response.json::<User>()?;
|
||||||
|
|||||||
@ -4,9 +4,10 @@ use serde::{Deserialize, Serialize};
|
|||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use tauri::{AppHandle, Manager};
|
use tauri::{AppHandle, Manager};
|
||||||
|
|
||||||
use crate::{auth::generate_authorization_header, AppState, DB};
|
|
||||||
use crate::db::DatabaseImpls;
|
|
||||||
use crate::db::DatabaseGameStatus;
|
use crate::db::DatabaseGameStatus;
|
||||||
|
use crate::db::DatabaseImpls;
|
||||||
|
use crate::remote::RemoteAccessError;
|
||||||
|
use crate::{auth::generate_authorization_header, AppState, DB};
|
||||||
|
|
||||||
#[derive(serde::Serialize)]
|
#[derive(serde::Serialize)]
|
||||||
struct FetchGameStruct {
|
struct FetchGameStruct {
|
||||||
@ -29,10 +30,9 @@ pub struct Game {
|
|||||||
m_image_library: Vec<String>,
|
m_image_library: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
fn fetch_library_logic(app: AppHandle) -> Result<String, RemoteAccessError> {
|
||||||
pub fn fetch_library(app: AppHandle) -> Result<String, String> {
|
|
||||||
let base_url = DB.fetch_base_url();
|
let base_url = DB.fetch_base_url();
|
||||||
let library_url = base_url.join("/api/v1/client/user/library").unwrap();
|
let library_url = base_url.join("/api/v1/client/user/library")?;
|
||||||
|
|
||||||
let header = generate_authorization_header();
|
let header = generate_authorization_header();
|
||||||
|
|
||||||
@ -40,18 +40,13 @@ pub fn fetch_library(app: AppHandle) -> Result<String, String> {
|
|||||||
let response = client
|
let response = client
|
||||||
.get(library_url.to_string())
|
.get(library_url.to_string())
|
||||||
.header("Authorization", header)
|
.header("Authorization", header)
|
||||||
.send()
|
.send()?;
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
if response.status() != 200 {
|
if response.status() != 200 {
|
||||||
return Err(format!(
|
return Err(response.status().as_u16().into());
|
||||||
"Library fetch request failed with {}",
|
|
||||||
response.status()
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keep as string
|
let games = response.json::<Vec<Game>>()?;
|
||||||
let games = response.json::<Vec<Game>>().unwrap();
|
|
||||||
|
|
||||||
let state = app.state::<Mutex<AppState>>();
|
let state = app.state::<Mutex<AppState>>();
|
||||||
let mut handle = state.lock().unwrap();
|
let mut handle = state.lock().unwrap();
|
||||||
@ -74,20 +69,48 @@ pub fn fetch_library(app: AppHandle) -> Result<String, String> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn fetch_game(id: String, app: tauri::AppHandle) -> Result<String, String> {
|
pub fn fetch_library(app: AppHandle) -> Result<String, String> {
|
||||||
|
let result = fetch_library_logic(app);
|
||||||
|
|
||||||
|
if result.is_err() {
|
||||||
|
return Err(result.err().unwrap().to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(result.unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fetch_game_logic(id: String, app: tauri::AppHandle) -> Result<String, RemoteAccessError> {
|
||||||
let state = app.state::<Mutex<AppState>>();
|
let state = app.state::<Mutex<AppState>>();
|
||||||
let handle = state.lock().unwrap();
|
let handle = state.lock().unwrap();
|
||||||
|
|
||||||
let game = handle.games.get(&id);
|
let game = handle.games.get(&id);
|
||||||
if let Some(game) = game {
|
if let Some(game) = game {
|
||||||
let db_handle = DB.borrow_data().unwrap();
|
let db_handle = DB.borrow_data().unwrap();
|
||||||
|
|
||||||
let data = FetchGameStruct {
|
let data = FetchGameStruct {
|
||||||
game: game.clone(),
|
game: game.clone(),
|
||||||
status: db_handle.games.games_statuses.get(&game.id).unwrap().clone(),
|
status: db_handle
|
||||||
|
.games
|
||||||
|
.games_statuses
|
||||||
|
.get(&game.id)
|
||||||
|
.unwrap()
|
||||||
|
.clone(),
|
||||||
};
|
};
|
||||||
|
|
||||||
return Ok(json!(data).to_string());
|
return Ok(json!(data).to_string());
|
||||||
}
|
}
|
||||||
|
// TODO request games that aren't found from remote server
|
||||||
|
|
||||||
Err("".to_string())
|
Err("".to_string().into())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub fn fetch_game(id: String, app: tauri::AppHandle) -> Result<String, String> {
|
||||||
|
let result = fetch_game_logic(id, app);
|
||||||
|
|
||||||
|
if result.is_err() {
|
||||||
|
return Err(result.err().unwrap().to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(result.unwrap())
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,48 +1,80 @@
|
|||||||
use std::sync::Mutex;
|
use std::{
|
||||||
|
fmt::{write, Display, Formatter},
|
||||||
|
sync::Mutex,
|
||||||
|
};
|
||||||
|
|
||||||
use log::{info, warn};
|
use log::{info, warn};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use url::Url;
|
use url::{ParseError, Url};
|
||||||
|
|
||||||
use crate::{AppState, AppStatus, DB};
|
use crate::{AppState, AppStatus, DB};
|
||||||
|
|
||||||
macro_rules! unwrap_or_return {
|
#[derive(Debug)]
|
||||||
( $e:expr ) => {
|
pub enum RemoteAccessError {
|
||||||
match $e {
|
FetchError(reqwest::Error),
|
||||||
Ok(x) => x,
|
ParsingError(ParseError),
|
||||||
Err(e) => {
|
InvalidCodeError(u16),
|
||||||
return Err(format!(
|
GenericErrror(String),
|
||||||
"Invalid URL or Drop is inaccessible ({})",
|
|
||||||
e.to_string()
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Display for RemoteAccessError {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
RemoteAccessError::FetchError(error) => write!(f, "{}", error),
|
||||||
|
RemoteAccessError::GenericErrror(error) => write!(f, "{}", error),
|
||||||
|
RemoteAccessError::ParsingError(parse_error) => {
|
||||||
|
write!(f, "{}", parse_error)
|
||||||
|
}
|
||||||
|
RemoteAccessError::InvalidCodeError(error) => write!(f, "HTTP {}", error),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<reqwest::Error> for RemoteAccessError {
|
||||||
|
fn from(err: reqwest::Error) -> Self {
|
||||||
|
RemoteAccessError::FetchError(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<String> for RemoteAccessError {
|
||||||
|
fn from(err: String) -> Self {
|
||||||
|
RemoteAccessError::GenericErrror(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<ParseError> for RemoteAccessError {
|
||||||
|
fn from(err: ParseError) -> Self {
|
||||||
|
RemoteAccessError::ParsingError(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl From<u16> for RemoteAccessError {
|
||||||
|
fn from(err: u16) -> Self {
|
||||||
|
RemoteAccessError::InvalidCodeError(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for RemoteAccessError {}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
#[serde(rename_all="camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
struct DropHealthcheck {
|
struct DropHealthcheck {
|
||||||
app_name: String,
|
app_name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
async fn use_remote_logic<'a>(
|
||||||
pub async fn use_remote<'a>(
|
|
||||||
url: String,
|
url: String,
|
||||||
state: tauri::State<'_, Mutex<AppState>>,
|
state: tauri::State<'_, Mutex<AppState>>,
|
||||||
) -> Result<(), String> {
|
) -> Result<(), RemoteAccessError> {
|
||||||
info!("connecting to url {}", url);
|
info!("connecting to url {}", url);
|
||||||
let base_url = unwrap_or_return!(Url::parse(&url));
|
let base_url = Url::parse(&url)?;
|
||||||
|
|
||||||
// Test Drop url
|
// Test Drop url
|
||||||
let test_endpoint = base_url.join("/api/v1").unwrap();
|
let test_endpoint = base_url.join("/api/v1")?;
|
||||||
let response = unwrap_or_return!(reqwest::get(test_endpoint.to_string()).await);
|
let response = reqwest::get(test_endpoint.to_string()).await?;
|
||||||
|
|
||||||
let result = response.json::<DropHealthcheck>().await.unwrap();
|
let result = response.json::<DropHealthcheck>().await?;
|
||||||
|
|
||||||
if result.app_name != "Drop" {
|
if result.app_name != "Drop" {
|
||||||
warn!("user entered drop endpoint that connected, but wasn't identified as Drop");
|
warn!("user entered drop endpoint that connected, but wasn't identified as Drop");
|
||||||
return Err("Not a valid Drop endpoint".to_string());
|
return Err("Not a valid Drop endpoint".to_string().into());
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut app_state = state.lock().unwrap();
|
let mut app_state = state.lock().unwrap();
|
||||||
@ -58,6 +90,20 @@ pub async fn use_remote<'a>(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tauri::command]
|
||||||
|
pub async fn use_remote<'a>(
|
||||||
|
url: String,
|
||||||
|
state: tauri::State<'_, Mutex<AppState>>,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
let result = use_remote_logic(url, state).await;
|
||||||
|
|
||||||
|
if result.is_err() {
|
||||||
|
return Err(result.err().unwrap().to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
pub fn gen_drop_url(path: String) -> Result<String, String> {
|
pub fn gen_drop_url(path: String) -> Result<String, String> {
|
||||||
let base_url = {
|
let base_url = {
|
||||||
|
|||||||
Reference in New Issue
Block a user