Skip to content

Commit 8afe238

Browse files
committed
handle err
1 parent a96930c commit 8afe238

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

app/worker.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,19 @@ self.onmessage = async function(event) {
99
type, secret, maxSide, outputAs
1010
);
1111

12-
if (type === 'encode') {
13-
const encoded = encode(buffer, secret, maxSide, outputAs);
14-
self.postMessage({ type: 'encoded', buffer: encoded, format: outputAs });
12+
try {
13+
if (type === 'encode') {
14+
const encoded = encode(buffer, secret, maxSide, outputAs);
15+
self.postMessage({ type: 'encoded', buffer: encoded, format: outputAs });
16+
}
17+
if (type === 'decode') {
18+
const decoded = decode(buffer, secret, maxSide, outputAs);
19+
self.postMessage({ type: 'decoded', buffer: decoded, format: outputAs });
20+
}
1521
}
16-
17-
if (type === 'decode') {
18-
const decoded = decode(buffer, secret, maxSide, outputAs);
19-
self.postMessage({ type: 'decoded', buffer: decoded, format: outputAs });
22+
catch (error) {
23+
console.error('Worker error:', error);
24+
self.postMessage({ error });
2025
}
26+
2127
}

src/lib.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ fn maybe_resize(img: image::RgbImage, max_side: u32) -> (image::RgbImage, u32, u
4646
}
4747
}
4848

49-
fn img2vec(im: &[u8], limit_max_side: Option<u32>) -> (Vec<u8>, ImageOptions) {
50-
let img = image::load_from_memory(im).expect("Failed to load image");
49+
fn img2vec(im: &[u8], limit_max_side: Option<u32>) -> Result<(Vec<u8>, ImageOptions), String> {
50+
let img = image::load_from_memory(im)
51+
.map_err(|e| format!("Failed to load image: {}", e))?;
5152
let mut rgb = img.to_rgb8();
5253

5354
if let Some(max_side) = limit_max_side {
@@ -61,19 +62,19 @@ fn img2vec(im: &[u8], limit_max_side: Option<u32>) -> (Vec<u8>, ImageOptions) {
6162
pixels.push(pixel[2]);
6263
};
6364

64-
(pixels, ImageOptions {
65+
Ok((pixels, ImageOptions {
6566
width: rgb.width(),
6667
height: rgb.height(),
6768
channels: 3,
68-
})
69+
}))
6970
}
7071

7172
enum ImageType {
7273
Png,
7374
Jpeg,
7475
}
7576

76-
fn vec2imblob(pixels: &Vec<u8>, im_opt: ImageOptions, limit_max_side: Option<u32>, im_type: ImageType) -> Box<[u8]> {
77+
fn vec2imblob(pixels: &Vec<u8>, im_opt: ImageOptions, limit_max_side: Option<u32>, im_type: ImageType) -> Result<Box<[u8]>, String> {
7778
let ImageOptions { mut width, mut height , channels} = im_opt;
7879
let mut img = image::RgbImage::new(width, height);
7980

@@ -96,28 +97,28 @@ fn vec2imblob(pixels: &Vec<u8>, im_opt: ImageOptions, limit_max_side: Option<u32
9697
ImageType::Png => {
9798
image::codecs::png::PngEncoder::new(&mut buf)
9899
.write_image(&img, width, height, image::ExtendedColorType::Rgb8)
99-
.expect("Failed to encode PNG image");
100+
.map_err(|e| format!("Failed to write PNG image - {}", e))?;
100101
},
101102
ImageType::Jpeg => {
102103
image::codecs::jpeg::JpegEncoder::new_with_quality(&mut buf, 80)
103104
.write_image(&img, width, height, image::ExtendedColorType::Rgb8)
104-
.expect("Failed to encode JPEG image");
105+
.map_err(|e| format!("Failed to write JPEG image - {}", e))?;
105106
},
106107
}
107108
console_log!("Export image, dimensions: {}x{}", width, height);
108109

109-
buf.into_boxed_slice()
110+
Ok(buf.into_boxed_slice())
110111
}
111112

112113
#[wasm_bindgen]
113-
pub fn encode(im: &[u8], secret: &str, max_side: i32, as_type: &str) -> Box<[u8]> {
114+
pub fn encode(im: &[u8], secret: &str, max_side: i32, as_type: &str) -> Result<Box<[u8]>, String> {
114115
console_log!("Encoding image with secret: {}, max_side: {}", secret, max_side);
115116
let max_side = if max_side < 1 { None } else { Some(max_side as u32) };
116117

117118
let seed = str2f(&secret);
118119
console_log!("Seed: {}", seed);
119120

120-
let (im_v, im_opt) = img2vec(im, max_side);
121+
let (im_v, im_opt) = img2vec(im, max_side)?;
121122
let pixels = logistic_map::encode::<3>(&im_v, seed);
122123
vec2imblob(&pixels, im_opt, None, match as_type {
123124
"png" => ImageType::Png,
@@ -127,14 +128,14 @@ pub fn encode(im: &[u8], secret: &str, max_side: i32, as_type: &str) -> Box<[u8]
127128
}
128129

129130
#[wasm_bindgen]
130-
pub fn decode(im: &[u8], secret: &str, max_side: i32, as_type: &str) -> Box<[u8]> {
131+
pub fn decode(im: &[u8], secret: &str, max_side: i32, as_type: &str) -> Result<Box<[u8]>, String> {
131132
console_log!("Decoding image with secret: {}, max_side: {}", secret, max_side);
132133
let max_side = if max_side < 1 { None } else { Some(max_side as u32) };
133134

134135
let seed = str2f(&secret);
135136
console_log!("Seed: {}", seed);
136137

137-
let (im_v, im_opt) = img2vec(im, None);
138+
let (im_v, im_opt) = img2vec(im, None)?;
138139
let pixels = logistic_map::decode::<3>(&im_v, seed);
139140
vec2imblob(&pixels, im_opt, max_side, match as_type {
140141
"png" => ImageType::Png,

0 commit comments

Comments
 (0)