sonic_rs/
error.rs

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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
//! Errors.

// The code is cloned from [serde_json](https://github.com/serde-rs/json) and modified necessary parts.

use core::{
    fmt::{self, Debug, Display},
    result,
};
use std::{borrow::Cow, error, fmt::Result as FmtResult, str::FromStr};

use serde::{
    de::{self, Unexpected},
    ser,
};
use sonic_number::Error as NumberError;
use thiserror::Error as ErrorTrait;

use crate::reader::Position;

/// This type represents all possible errors that can occur when serializing or
/// deserializing JSON data.
pub struct Error {
    /// This `Box` allows us to keep the size of `Error` as small as possible. A
    /// larger `Error` type was substantially slower due to all the functions
    /// that pass around `Result<T, Error>`.
    err: Box<ErrorImpl>,
}

/// Alias for a `Result` with the error type `sonic_rs::Error`.
pub type Result<T> = result::Result<T, Error>;

impl Error {
    /// One-based line number at which the error was detected.
    ///
    /// Characters in the first line of the input (before the first newline
    /// character) are in line 1.
    pub fn line(&self) -> usize {
        self.err.line
    }

    /// One-based column number at which the error was detected.
    ///
    /// The first character in the input and any characters immediately
    /// following a newline character are in column 1.
    ///
    /// Note that errors may occur in column 0, for example if a read from an
    /// I/O stream fails immediately following a previously read newline
    /// character.
    pub fn column(&self) -> usize {
        self.err.column
    }

    /// The kind reported by the underlying standard library I/O error, if this
    /// error was caused by a failure to read or write bytes on an I/O stream.
    pub fn io_error_kind(&self) -> Option<std::io::ErrorKind> {
        if let ErrorCode::Io(io_error) = &self.err.code {
            Some(io_error.kind())
        } else {
            None
        }
    }

    /// Categorizes the cause of this error.
    ///
    /// - `Category::Io` - failure to read or write bytes on an I/O stream
    /// - `Category::Syntax` - input that is not syntactically valid JSON
    /// - `Category::Data` - input data that is semantically incorrect
    /// - `Category::Eof` - unexpected end of the input data
    pub fn classify(&self) -> Category {
        match self.err.code {
            ErrorCode::Message(_) | ErrorCode::UnexpectedVisitType => Category::TypeUnmatched,
            ErrorCode::GetInEmptyObject
            | ErrorCode::GetInEmptyArray
            | ErrorCode::GetIndexOutOfArray
            | ErrorCode::GetUnknownKeyInObject => Category::NotFound,
            ErrorCode::Io(_) => Category::Io,
            ErrorCode::EofWhileParsing => Category::Eof,
            ErrorCode::ExpectedColon
            | ErrorCode::ExpectedObjectCommaOrEnd
            | ErrorCode::InvalidEscape
            | ErrorCode::InvalidJsonValue
            | ErrorCode::InvalidLiteral
            | ErrorCode::InvalidUTF8
            | ErrorCode::InvalidNumber
            | ErrorCode::NumberOutOfRange
            | ErrorCode::InvalidUnicodeCodePoint
            | ErrorCode::ControlCharacterWhileParsingString
            | ErrorCode::TrailingComma
            | ErrorCode::TrailingCharacters
            | ErrorCode::ExpectObjectKeyOrEnd
            | ErrorCode::ExpectedArrayCommaOrEnd
            | ErrorCode::ExpectedArrayStart
            | ErrorCode::ExpectedObjectStart
            | ErrorCode::InvalidSurrogateUnicodeCodePoint
            | ErrorCode::SerExpectKeyIsStrOrNum(_)
            | ErrorCode::FloatMustBeFinite
            | ErrorCode::ExpectedQuote
            | ErrorCode::ExpectedNumericKey
            | ErrorCode::RecursionLimitExceeded => Category::Syntax,
        }
    }

    /// Returns true if this error was caused by a failure to read or write
    /// bytes on an I/O stream.
    pub fn is_io(&self) -> bool {
        self.classify() == Category::Io
    }

    /// Returns true if this error was caused by input that was not
    /// syntactically valid JSON.
    pub fn is_syntax(&self) -> bool {
        self.classify() == Category::Syntax
    }

    /// Returns true when the input data is unmatched for expected type.
    ///
    /// For example, JSON containing a number  when the type being deserialized into holds a String.
    pub fn is_unmatched_type(&self) -> bool {
        self.classify() == Category::TypeUnmatched
    }

    /// Return true when the target field was not found from JSON.
    ///
    /// For example:
    /// When using `get*` APIs, it gets a unknown keys from JSON text, or get
    /// a index out of the array.
    pub fn is_not_found(&self) -> bool {
        self.classify() == Category::NotFound
    }

    /// Returns true if this error was caused by prematurely reaching the end of
    /// the input data.
    ///
    /// Callers that process streaming input may be interested in retrying the
    /// deserialization once more data is available.
    pub fn is_eof(&self) -> bool {
        self.classify() == Category::Eof
    }

    /// Returens the offset of the error position from the starting of JSON text.
    pub fn offset(&self) -> usize {
        self.err.index
    }
}

#[allow(clippy::fallible_impl_from)]
impl From<Error> for std::io::Error {
    /// Convert a `sonic_rs::Error` into an `std::io::Error`.
    ///
    /// JSON syntax and data errors are turned into `InvalidData` I/O errors.
    /// EOF errors are turned into `UnexpectedEof` I/O errors.
    fn from(j: Error) -> Self {
        match j.err.code {
            ErrorCode::Io(err) => err,
            ErrorCode::EofWhileParsing => std::io::Error::new(std::io::ErrorKind::UnexpectedEof, j),
            _ => std::io::Error::new(std::io::ErrorKind::InvalidData, j),
        }
    }
}

/// Categorizes the cause of a `sonic_rs::Error`.
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub enum Category {
    /// The error was caused by a failure to read or write bytes on an I/O
    /// stream.
    /// TODO: support stream reader in the future
    Io,

    /// The error was caused by input that was not syntactically valid JSON.
    Syntax,

    /// The error was caused when the input data is unmatched for expected type.
    ///
    /// For example, JSON containing a number  when the type being deserialized into holds a
    /// String.
    TypeUnmatched,

    /// The error was caused when the target field was not found from JSON.
    ///
    /// For example:
    /// When using `get*` APIs, it gets a unknown keys from JSON text, or get
    /// a index out of the array.
    NotFound,

    /// The error was caused by prematurely reaching the end of the input data.
    ///
    /// Callers that process streaming input may be interested in retrying the
    /// deserialization once more data is available.
    Eof,
}

struct ErrorImpl {
    code: ErrorCode,
    index: usize,
    line: usize,
    column: usize,
    // the descript of the error position
    descript: Option<String>,
}

#[derive(ErrorTrait, Debug)]
pub(crate) enum ErrorCode {
    #[error("{0}")]
    Message(Cow<'static, str>),

    #[error("io error while serializing or deserializing")]
    Io(std::io::Error),

    #[error("EOF while parsing")]
    EofWhileParsing,

    #[error("Expected this character to be a ':' while parsing")]
    ExpectedColon,

    #[error("Expected this character to be either a ',' or a ']' while parsing")]
    ExpectedArrayCommaOrEnd,

    #[error("Expected this character to be either a ',' or a '}}' while parsing")]
    ExpectedObjectCommaOrEnd,

    #[error("Invalid literal (`true`, `false`, or a `null`) while parsing")]
    InvalidLiteral,

    #[error("Invalid JSON value")]
    InvalidJsonValue,

    #[error("Expected this character to be '{{'")]
    ExpectedObjectStart,

    #[error("Expected this character to be '['")]
    ExpectedArrayStart,

    #[error("Invalid escape chars")]
    InvalidEscape,

    #[error("Invalid number")]
    InvalidNumber,

    #[error("Number is bigger than the maximum value of its type")]
    NumberOutOfRange,

    #[error("Invalid unicode code point")]
    InvalidUnicodeCodePoint,

    #[error("Invalid UTF-8 characters in json")]
    InvalidUTF8,

    #[error("Control character found while parsing a string")]
    ControlCharacterWhileParsingString,

    #[error("Expected this character to be '\"' or '}}'")]
    ExpectObjectKeyOrEnd,

    #[error("JSON has a comma after the last value in an array or object")]
    TrailingComma,

    #[error("JSON has non-whitespace trailing characters after the value")]
    TrailingCharacters,

    #[error("Encountered nesting of JSON maps and arrays more than 128 layers deep")]
    RecursionLimitExceeded,

    #[error("Get value from an empty object")]
    GetInEmptyObject,

    #[error("Get unknown key from the object")]
    GetUnknownKeyInObject,

    #[error("Get value from an empty array")]
    GetInEmptyArray,

    #[error("Get index out of the array")]
    GetIndexOutOfArray,

    #[error("Unexpected visited type in JSON visitor")]
    UnexpectedVisitType,

    #[error("Invalid surrogate Unicode code point")]
    InvalidSurrogateUnicodeCodePoint,

    #[error("Float number must be finite, not be Infinity or NaN")]
    FloatMustBeFinite,

    #[error("Expect a numeric key in Value")]
    ExpectedNumericKey,

    #[error("Expect a quote")]
    ExpectedQuote,

    #[error("Expected the key to be string/bool/number when serializing map, now is {0}")]
    SerExpectKeyIsStrOrNum(Unexpected<'static>),
}

impl From<NumberError> for ErrorCode {
    fn from(err: NumberError) -> Self {
        match err {
            NumberError::InvalidNumber => ErrorCode::InvalidNumber,
            NumberError::FloatMustBeFinite => ErrorCode::FloatMustBeFinite,
        }
    }
}

impl Error {
    #[cold]
    pub(crate) fn syntax(code: ErrorCode, json: &[u8], index: usize) -> Self {
        let position = Position::from_index(index, json);
        // generate descript about 16 characters
        let mut start = if index < 8 { 0 } else { index - 8 };
        let mut end = if index + 8 > json.len() {
            json.len()
        } else {
            index + 8
        };

        // find the nearest valid utf-8 character
        while start > 0 && index - start <= 16 && (json[start] & 0b1100_0000) == 0b1000_0000 {
            start -= 1;
        }

        // find the nearest valid utf-8 character
        while end < json.len() && end - index <= 16 && (json[end - 1] & 0b1100_0000) == 0b1000_0000
        {
            end += 1;
        }

        let fragment = String::from_utf8_lossy(&json[start..end]).to_string();
        let left = index - start;
        let right = if end - index > 1 {
            end - (index + 1)
        } else {
            0
        };
        let mask = ".".repeat(left) + "^" + &".".repeat(right);
        let descript = format!("\n\n\t{}\n\t{}\n", fragment, mask);

        Error {
            err: Box::new(ErrorImpl {
                code,
                line: position.line,
                column: position.column,
                index,
                descript: Some(descript),
            }),
        }
    }

    #[cold]
    pub(crate) fn ser_error(code: ErrorCode) -> Self {
        Error {
            err: Box::new(ErrorImpl {
                code,
                line: 0,
                column: 0,
                index: 0,
                descript: None,
            }),
        }
    }

    #[cold]
    pub(crate) fn io(error: std::io::Error) -> Self {
        Error {
            err: Box::new(ErrorImpl {
                code: ErrorCode::Io(error),
                line: 0,
                index: 0,
                column: 0,
                descript: None,
            }),
        }
    }

    #[cold]
    pub(crate) fn error_code(self) -> ErrorCode {
        self.err.code
    }
}

impl serde::de::StdError for Error {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match &self.err.code {
            ErrorCode::Io(err) => err.source(),
            _ => None,
        }
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> FmtResult {
        Display::fmt(&*self.err, f)
    }
}

impl Display for ErrorImpl {
    fn fmt(&self, f: &mut fmt::Formatter) -> FmtResult {
        if self.line != 0 {
            write!(
                f,
                "{} at line {} column {}{}",
                self.code,
                self.line,
                self.column,
                self.descript.as_ref().unwrap_or(&"".to_string())
            )
        } else {
            write!(f, "{}", self.code)
        }
    }
}

// Remove two layers of verbosity from the debug representation. Humans often
// end up seeing this representation because it is what unwrap() shows.
impl Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> FmtResult {
        Display::fmt(&self, f)
    }
}

impl de::Error for Error {
    #[cold]
    fn custom<T: Display>(msg: T) -> Error {
        make_error(msg.to_string())
    }

    #[cold]
    fn invalid_type(unexp: de::Unexpected, exp: &dyn de::Expected) -> Self {
        if let de::Unexpected::Unit = unexp {
            Error::custom(format_args!("invalid type: null, expected {}", exp))
        } else {
            Error::custom(format_args!("invalid type: {}, expected {}", unexp, exp))
        }
    }
}

impl ser::Error for Error {
    #[cold]
    fn custom<T: Display>(msg: T) -> Error {
        make_error(msg.to_string())
    }
}

// TODO: remove me in 0.4 version.
#[cold]
pub fn make_error(mut msg: String) -> Error {
    let (line, column) = parse_line_col(&mut msg).unwrap_or((0, 0));
    Error {
        err: Box::new(ErrorImpl {
            code: ErrorCode::Message(msg.into()),
            line,
            index: 0,
            column,
            descript: None,
        }),
    }
}

fn parse_line_col(msg: &mut String) -> Option<(usize, usize)> {
    let start_of_suffix = match msg.rfind(" at line ") {
        Some(index) => index,
        None => return None,
    };

    // Find start and end of line number.
    let start_of_line = start_of_suffix + " at line ".len();
    let mut end_of_line = start_of_line;
    while starts_with_digit(&msg[end_of_line..]) {
        end_of_line += 1;
    }

    if !msg[end_of_line..].starts_with(" column ") {
        return None;
    }

    // Find start and end of column number.
    let start_of_column = end_of_line + " column ".len();
    let mut end_of_column = start_of_column;
    while starts_with_digit(&msg[end_of_column..]) {
        end_of_column += 1;
    }

    if end_of_column < msg.len() {
        return None;
    }

    // Parse numbers.
    let line = match usize::from_str(&msg[start_of_line..end_of_line]) {
        Ok(line) => line,
        Err(_) => return None,
    };
    let column = match usize::from_str(&msg[start_of_column..end_of_column]) {
        Ok(column) => column,
        Err(_) => return None,
    };

    msg.truncate(start_of_suffix);
    Some((line, column))
}

fn starts_with_digit(slice: &str) -> bool {
    match slice.as_bytes().first() {
        None => false,
        Some(&byte) => byte.is_ascii_digit(),
    }
}

pub(crate) fn invalid_utf8(json: &[u8], index: usize) -> Error {
    Error::syntax(ErrorCode::InvalidUTF8, json, index)
}

#[cfg(test)]
mod test {
    use crate::{from_slice, from_str, Deserialize};

    #[test]
    fn test_serde_errors_display() {
        #[allow(unused)]
        #[derive(Debug, Deserialize)]
        struct Foo {
            a: Vec<i32>,
            c: String,
        }

        let err = from_str::<Foo>("{ \"b\":[]}").unwrap_err();
        assert_eq!(
            format!("{}", err),
            "missing field `a` at line 1 column 8\n\n\t{ \"b\":[]}\n\t........^\n"
        );

        let err = from_str::<Foo>("{\"a\": [1, 2x, 3, 4, 5]}").unwrap_err();
        println!("{}", err);
        assert_eq!(
            format!("{}", err),
            "Expected this character to be either a ',' or a ']' while parsing at line 1 column \
             11\n\n\t\": [1, 2x, 3, 4,\n\t........^.......\n"
        );

        let err = from_str::<Foo>("{\"a\": null}").unwrap_err();
        assert_eq!(
            format!("{}", err),
            "invalid type: null, expected a sequence at line 1 column 9\n\n\t\"a\": \
             null}\n\t........^.\n"
        );

        let err = from_str::<Foo>("{\"a\": [1,2,3  }").unwrap_err();
        assert_eq!(
            format!("{}", err),
            "Expected this character to be either a ',' or a ']' while parsing at line 1 column \
             14\n\n\t[1,2,3  }\n\t........^\n"
        );

        let err = from_str::<Foo>("{\"a\": [\"123\"]}").unwrap_err();
        assert_eq!(
            format!("{}", err),
            "invalid type: string \"123\", expected i32 at line 1 column 11\n\n\t\": \
             [\"123\"]}\n\t........^..\n"
        );

        let err = from_str::<Foo>("{\"a\": [").unwrap_err();
        assert_eq!(
            format!("{}", err),
            "EOF while parsing at line 1 column 6\n\n\t{\"a\": [\n\t......^\n"
        );

        let err = from_str::<Foo>("{\"a\": [000]}").unwrap_err();
        assert_eq!(
            format!("{}", err),
            "Expected this character to be either a ',' or a ']' while parsing at line 1 column \
             8\n\n\t{\"a\": [000]}\n\t........^...\n"
        );

        let err = from_str::<Foo>("{\"a\": [-]}").unwrap_err();
        assert_eq!(
            format!("{}", err),
            "Invalid number at line 1 column 7\n\n\t{\"a\": [-]}\n\t.......^..\n"
        );

        let err = from_str::<Foo>("{\"a\": [-1.23e]}").unwrap_err();
        assert_eq!(
            format!("{}", err),
            "Invalid number at line 1 column 12\n\n\t: [-1.23e]}\n\t........^..\n"
        );

        let err = from_str::<Foo>("{\"c\": \"哈哈哈哈哈哈}").unwrap_err();
        assert_eq!(
            format!("{}", err),
            "EOF while parsing at line 1 column 25\n\n\t哈哈哈}\n\t.........^\n"
        );

        let err = from_slice::<Foo>(b"{\"b\":\"\x80\"}").unwrap_err();
        assert_eq!(
            format!("{}", err),
            "Invalid UTF-8 characters in json at line 1 column 6\n\n\t{\"b\":\"�\"}\n\t......^..\n"
        );
    }

    #[test]
    fn test_other_errors() {
        let err = crate::Value::try_from(f64::NAN).unwrap_err();
        assert_eq!(
            format!("{}", err),
            "NaN or Infinity is not a valid JSON value"
        );
    }
}