1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use std::{fmt, ops::Deref};

use crate::{convert::Coerced, qjs, Ctx, Error, FromJs, IntoJs, Object, Result, Value};

/// A javascript instance of Error
///
/// Will turn into a error when converted to javascript but won't autmatically be thrown.
#[repr(transparent)]
pub struct Exception<'js>(Object<'js>);

impl fmt::Debug for Exception<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Exception")
            .field("object", &self.0)
            .field("message", &self.message())
            .field("file", &self.file())
            .field("line", &self.line())
            .field("stack", &self.stack())
            .finish()
    }
}

impl<'js> Exception<'js> {
    /// Turns the exception into the underlying object.
    pub fn into_object(self) -> Object<'js> {
        self.0
    }

    /// Returns a reference to the underlying object.
    pub fn as_object(&self) -> &Object<'js> {
        &self.0
    }

    pub fn into_value(self) -> Value<'js> {
        self.0.into_value()
    }

    /// Creates an exception from an object if it is an instance of error.
    pub fn from_object(obj: Object<'js>) -> Option<Self> {
        if obj.is_error() {
            Some(Self(obj))
        } else {
            None
        }
    }

    /// Creates a new exception with a give message.
    pub fn from_message(ctx: Ctx<'js>, message: &str) -> Result<Self> {
        let obj = unsafe {
            let value = ctx.handle_exception(qjs::JS_NewError(ctx.as_ptr()))?;
            Value::from_js_value(ctx, value)
                .into_object()
                .expect("`JS_NewError` did not return an object")
        };
        obj.set("message", message)?;
        Ok(Exception(obj))
    }

    pub fn message(&self) -> Option<String> {
        self.get::<_, Option<Coerced<String>>>("message")
            .ok()
            .and_then(|x| x)
            .map(|x| x.0)
    }

    pub fn file(&self) -> Option<String> {
        self.get::<_, Option<Coerced<String>>>("fileName")
            .ok()
            .and_then(|x| x)
            .map(|x| x.0)
    }

    pub fn line(&self) -> Option<i32> {
        self.get::<_, Option<Coerced<i32>>>("lineNumber")
            .ok()
            .and_then(|x| x)
            .map(|x| x.0)
    }

    pub fn stack(&self) -> Option<String> {
        self.get::<_, Option<Coerced<String>>>("stack")
            .ok()
            .and_then(|x| x)
            .map(|x| x.0)
    }

    /// Sets the exception as the current error an returns `Error::Exception`
    pub fn throw(self) -> Error {
        self.0.ctx.throw(self.0.into_value())
    }
}

impl fmt::Display for Exception<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        "Exception generated by quickjs: ".fmt(f)?;
        if let Some(file) = self.file() {
            '['.fmt(f)?;
            file.fmt(f)?;
            ']'.fmt(f)?;
        }
        if let Some(line) = self.line() {
            ':'.fmt(f)?;
            line.fmt(f)?;
        }
        if let Some(message) = self.message() {
            ' '.fmt(f)?;
            message.fmt(f)?;
        }
        if let Some(stack) = self.stack() {
            '\n'.fmt(f)?;
            stack.fmt(f)?;
        }
        Ok(())
    }
}

impl<'js> Deref for Exception<'js> {
    type Target = Object<'js>;

    fn deref(&self) -> &Self::Target {
        self.as_object()
    }
}

impl<'js> FromJs<'js> for Exception<'js> {
    fn from_js(_ctx: crate::Ctx<'js>, value: Value<'js>) -> Result<Self> {
        if let Some(obj) = value.as_object() {
            if obj.is_error() {
                return Ok(Exception(obj.clone()));
            } else {
                return Err(Error::FromJs {
                    from: value.type_name(),
                    to: "Exception",
                    message: Some("object was not an instance of error".to_string()),
                });
            }
        }
        return Err(Error::FromJs {
            from: value.type_name(),
            to: "Exception",
            message: Some("value was not a type".to_string()),
        });
    }
}

impl<'js> IntoJs<'js> for Exception<'js> {
    fn into_js(self, _ctx: crate::Ctx<'js>) -> Result<Value<'js>> {
        Ok(self.0.into_value())
    }
}