opensrv_clickhouse/protocols/protocol_exception.rs
1// Copyright 2021 Datafuse Labs.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::binary::Encoder;
16use crate::error_codes::UNKNOWN_EXCEPTION;
17use crate::errors::Error;
18use crate::protocols::*;
19
20pub struct ExceptionResponse {}
21
22impl ExceptionResponse {
23 pub fn write(encoder: &mut Encoder, error: &Error, with_stack_trace: bool) {
24 let mut code = UNKNOWN_EXCEPTION;
25 let name = error.exception_name();
26 let mut stack_trace = "".to_string();
27 let mut message = error.to_string();
28
29 if let Error::Server(e) = error {
30 code = e.code;
31 if with_stack_trace {
32 stack_trace = e.stack_trace.clone();
33 }
34 message = e.message.clone();
35 }
36 encoder.uvarint(SERVER_EXCEPTION);
37
38 encoder.write(code);
39 //Name
40 encoder.string(name);
41 // Message
42 encoder.string(message);
43 // StackTrace
44 encoder.string(stack_trace);
45 // Nested.
46 encoder.write(false);
47 }
48}