summary refs log tree commit diff
path: root/rust/src/errors.rs
blob: 4e580e3e8cbc23d16ae501a087d58183345424c6 (plain) (blame)
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
/*
 * This file is licensed under the Affero General Public License (AGPL) version 3.
 *
 * Copyright (C) 2024 New Vector, Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * See the GNU Affero General Public License for more details:
 * <https://www.gnu.org/licenses/agpl-3.0.html>.
 */

#![allow(clippy::new_ret_no_self)]

use std::collections::HashMap;

use http::{HeaderMap, StatusCode};
use pyo3::{exceptions::PyValueError, import_exception};

import_exception!(synapse.api.errors, SynapseError);

impl SynapseError {
    pub fn new(
        code: StatusCode,
        message: String,
        errcode: &'static str,
        additional_fields: Option<HashMap<String, String>>,
        headers: Option<HeaderMap>,
    ) -> pyo3::PyErr {
        // Transform the HeaderMap into a HashMap<String, String>
        let headers = if let Some(headers) = headers {
            let mut map = HashMap::with_capacity(headers.len());
            for (key, value) in headers.iter() {
                let Ok(value) = value.to_str() else {
                    // This should never happen, but we don't want to panic in case it does
                    return PyValueError::new_err(
                        "Could not construct SynapseError: header value is not valid ASCII",
                    );
                };

                map.insert(key.as_str().to_owned(), value.to_owned());
            }
            Some(map)
        } else {
            None
        };

        SynapseError::new_err((code.as_u16(), message, errcode, additional_fields, headers))
    }
}

import_exception!(synapse.api.errors, NotFoundError);

impl NotFoundError {
    pub fn new() -> pyo3::PyErr {
        NotFoundError::new_err(())
    }
}