Skip to main content

mz_pgrepr/value/
error.rs

1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! Error types for decoding wire-format values and for fallible conversion
11//! from [`super::Value`] to [`mz_repr::Datum`].
12
13use std::error::Error;
14use std::fmt;
15
16use mz_repr::adt::array::InvalidArrayError;
17use mz_repr::adt::range::InvalidRangeError;
18
19/// Error returned when a decoded text value contains a NUL character, which
20/// PostgreSQL-compatible text values must never contain.
21#[derive(Debug)]
22pub struct NulCharacterError;
23
24impl fmt::Display for NulCharacterError {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        // This matches PostgreSQL's message for NUL bytes in text values.
27        f.write_str("invalid byte sequence for encoding \"UTF8\": 0x00")
28    }
29}
30
31impl Error for NulCharacterError {}
32
33/// Errors that can occur when converting a [`super::Value`] into a [`mz_repr::Datum`].
34#[derive(Debug)]
35pub enum IntoDatumError {
36    /// Invalid range (e.g. misordered bounds).
37    Range(InvalidRangeError),
38    /// Invalid array (e.g. wrong cardinality or too many dimensions).
39    Array(InvalidArrayError),
40}
41
42impl fmt::Display for IntoDatumError {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        match self {
45            IntoDatumError::Range(e) => e.fmt(f),
46            IntoDatumError::Array(e) => e.fmt(f),
47        }
48    }
49}
50
51impl Error for IntoDatumError {
52    fn source(&self) -> Option<&(dyn Error + 'static)> {
53        match self {
54            IntoDatumError::Range(e) => Some(e),
55            IntoDatumError::Array(e) => Some(e),
56        }
57    }
58}
59
60impl From<InvalidRangeError> for IntoDatumError {
61    fn from(e: InvalidRangeError) -> Self {
62        IntoDatumError::Range(e)
63    }
64}
65
66impl From<InvalidArrayError> for IntoDatumError {
67    fn from(e: InvalidArrayError) -> Self {
68        IntoDatumError::Array(e)
69    }
70}