1
Fork 0
mirror of https://github.com/redstrate/Auracite.git synced 2025-04-24 13:37:45 +00:00

Bubble up errors to JS

This commit is contained in:
Joshua Goins 2024-10-31 19:56:08 -04:00
parent 489084cb51
commit 5882bdd8f7
4 changed files with 25 additions and 11 deletions

View file

@ -13,6 +13,9 @@
archive_character_base64(document.getElementById("name").value, false).then((uri) => {
// Download character archive
window.location.replace(uri);
document.getElementById("statusMessage").innerText = "Archive complete!";
}).catch((err) => {
document.getElementById("statusMessage").innerText = err;
});
});
}
@ -54,5 +57,6 @@
<label for="scales">Connect to Dalamud Plugin</label>
<button class="favorite styled" type="button" id="downloadButton">Download</button>
<a href="https://github.com/redstrate/Auracite">Source Code</a>
<p id="statusMessage"></p>
</body>
</html>

View file

@ -1,13 +1,12 @@
use reqwest::Url;
pub async fn download(url: &Url) -> Result<Vec<u8>, ()> {
pub async fn download(url: &Url) -> Result<Vec<u8>, reqwest::Error> {
let client = reqwest::Client::builder()
.build()
.unwrap();
.build()?;
let body = client.get(url.to_string())
.send()
.await;
Ok(body.unwrap().bytes().await.unwrap().to_vec())
Ok(body?.bytes().await?.to_vec())
}

View file

@ -1,7 +1,5 @@
use crate::data::CharacterData;
use minijinja::{context, Environment};
use std::fs::write;
use std::io;
/// Writes a visual HTML for `char_data` to `file_path`.
/// This vaguely represents Lodestone and designed to visually check your character data.

View file

@ -20,6 +20,8 @@ use crate::parser::parse_search;
use base64::prelude::*;
#[cfg(target_family = "wasm")]
use wasm_bindgen::prelude::wasm_bindgen;
#[cfg(target_family = "wasm")]
use wasm_bindgen::JsValue;
const LODESTONE_HOST: &str = "https://na.finalfantasyxiv.com";
@ -83,6 +85,19 @@ impl From<std::io::Error> for ArchiveError {
}
}
#[cfg(target_family = "wasm")]
impl From<ArchiveError> for JsValue {
fn from(err: ArchiveError) -> Self {
match err {
// TODO: give JS the URL that failed to download
ArchiveError::DownloadFailed(_) => { JsValue::from_str(&"download_failed".to_string()) }
ArchiveError::CharacterNotFound => { JsValue::from_str(&"character_not_found".to_string()) }
ArchiveError::ParsingError => { JsValue::from_str(&"parsing_error".to_string())}
ArchiveError::UnknownError => { JsValue::from_str(&"unknown_error".to_string()) }
}
}
}
/// Archives the character named `character_name` and gives a ZIP file as bytes that can be written to disk.
pub async fn archive_character(character_name: &str, use_dalamud: bool) -> Result<Vec<u8>, ArchiveError> {
let search_url = Url::parse_with_params(&format!("{LODESTONE_HOST}/lodestone/character?"), &[("q", character_name)]).map_err(|_| ArchiveError::UnknownError)?;
@ -170,9 +185,7 @@ pub async fn archive_character(character_name: &str, use_dalamud: bool) -> Resul
/// Archives the character named `character_name` and converts the ZIP file to Base64. Useful for downloading via data URIs.
#[cfg(target_family = "wasm")]
#[wasm_bindgen]
pub async extern fn archive_character_base64(character_name: &str, use_dalamud: bool) -> String {
let buf = archive_character(character_name, use_dalamud).await;
let base64 = BASE64_STANDARD.encode(buf);
return format!("data:application/octet-stream;charset=utf-16le;base64,{base64}").into();
pub async extern fn archive_character_base64(character_name: &str, use_dalamud: bool) -> Result<String, ArchiveError> {
let buf: String = archive_character(character_name, use_dalamud).await.map(|x| BASE64_STANDARD.encode(x))?;
return Ok(format!("data:application/octet-stream;charset=utf-16le;base64,{buf}").into());
}