ssb_validate/
error.rs

1//! Custom error type with all possible variants for this library.
2//!
3//! Error variants are imported into other crate modules as required. The [`snafu`](https://docs.rs/snafu/0.6.10) crate is
4//! used here to "assign underlying errors into domain-specific errors while adding context." Note that the visibility
5//! attribute has been applied to `Error` to make the variants useable throughout the crate (see the `snafu` documentation
6//! on [Controlling Visibility](https://docs.rs/snafu/0.6.10/snafu/guide/attributes/index.html#controlling-visibility)
7//! for more information). This approach deviates from the recommended usage of the snafu library but has been taken here
8//! to simplify reasoning about error-handling in this library.
9use snafu::Snafu;
10use ssb_legacy_msg_data::json::{DecodeJsonError, EncodeJsonError};
11use ssb_multiformats::multihash::Multihash;
12
13pub type Result<T, E = Error> = std::result::Result<T, E>;
14
15#[derive(Debug, Snafu)]
16#[snafu(visibility = "pub(crate)")]
17pub enum Error {
18    #[snafu(display("Previous message was invalid. Decoding failed with: {}", source))]
19    InvalidPreviousMessage {
20        source: DecodeJsonError,
21        message: Vec<u8>,
22    },
23    #[snafu(display("Message was invalid. Decoding failed with: {}", source))]
24    InvalidMessage {
25        source: DecodeJsonError,
26        message: Vec<u8>,
27    },
28    #[snafu(display("Message must have keys in correct order",))]
29    InvalidMessageValueOrder { message: Vec<u8> },
30    #[snafu(display(
31        "Message was invalid. The authors did not match. \nAuthor of previous: {}\n Author: {} ",
32        previous_author,
33        author
34    ))]
35    AuthorsDidNotMatch {
36        previous_author: String,
37        author: String,
38    },
39    #[snafu(display("The first message of a feed must have seq of 1",))]
40    FirstMessageDidNotHaveSequenceOfOne { message: Vec<u8> },
41    #[snafu(display("The first message of a feed must have previous of null",))]
42    FirstMessageDidNotHavePreviousOfNull { message: Vec<u8> },
43    #[snafu(display("The message hash must be 'sha256'",))]
44    InvalidHashFunction { message: Vec<u8> },
45    #[snafu(display("The message content string must be canonical base64",))]
46    InvalidBase64 { message: Vec<u8> },
47    #[snafu(display("The message value must not be longer than 8192 UTF-16 code units",))]
48    InvalidMessageValueLength { message: Vec<u8> },
49    #[snafu(display("The sequence must increase by one",))]
50    InvalidSequenceNumber {
51        message: Vec<u8>,
52        actual: u64,
53        expected: u64,
54    },
55    #[snafu(display("Unable to get the value from the message, the message was invalid"))]
56    InvalidMessageNoValue,
57    #[snafu(display("Could not serialize message.value to bytes. Failed with: {}", source))]
58    InvalidMessageCouldNotSerializeValue { source: EncodeJsonError },
59    #[snafu(display("The actual hash of the value did not match the hash claimed by `key`"))]
60    ActualHashDidNotMatchKey {
61        message: Vec<u8>,
62        actual_hash: Multihash,
63        expected_hash: Multihash,
64    },
65    #[snafu(display("Previous was set to null but it should have had a value"))]
66    PreviousWasNull,
67    #[snafu(display(
68        "This feed is forked. Last known good message was as seq: {}",
69        previous_seq
70    ))]
71    ForkedFeed { previous_seq: u64 },
72}