Skip to content

Commit 3b8fc56

Browse files
committed
Move errors to shared
1 parent 1e8b375 commit 3b8fc56

File tree

10 files changed

+63
-29
lines changed

10 files changed

+63
-29
lines changed

codegen/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ mod sqlstate;
99
mod type_gen;
1010

1111
fn main() {
12-
let path = Path::new("../src");
12+
let path = Path::new("..");
1313
sqlstate::build(path);
1414
type_gen::build(path);
1515
}

codegen/src/sqlstate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct Code {
1313
}
1414

1515
pub fn build(path: &Path) {
16-
let mut file = BufWriter::new(File::create(path.join("error/sqlstate.rs")).unwrap());
16+
let mut file = BufWriter::new(File::create(path.join("postgres-shared/src/error/sqlstate.rs")).unwrap());
1717

1818
let codes = parse_codes();
1919

codegen/src/type_gen.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ struct Type {
2020
}
2121

2222
pub fn build(path: &Path) {
23-
let mut file = BufWriter::new(File::create(path.join("types/type_gen.rs")).unwrap());
23+
let mut file = BufWriter::new(File::create(path.join("postgres/src/types/type_gen.rs")).unwrap());
2424

2525
let ranges = parse_ranges();
2626
let types = parse_types(&ranges);

postgres-shared/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ authors = ["Steven Fackler <[email protected]>"]
55

66
[dependencies]
77
hex = "0.2"
8+
fallible-iterator = "0.1.3"
9+
phf = "=0.7.20"
10+
postgres-protocol = "0.2"

postgres/src/error/mod.rs renamed to postgres-shared/src/error/mod.rs

+42-14
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,8 @@ use std::error;
66
use std::convert::From;
77
use std::fmt;
88
use std::io;
9-
use std::result;
109

1110
pub use self::sqlstate::SqlState;
12-
use {Result, DbErrorNew};
1311

1412
mod sqlstate;
1513

@@ -141,8 +139,9 @@ pub struct DbError {
141139
_p: (),
142140
}
143141

144-
impl DbErrorNew for DbError {
145-
fn new_raw(fields: &mut ErrorFields) -> io::Result<DbError> {
142+
impl DbError {
143+
#[doc(hidden)]
144+
pub fn new_raw(fields: &mut ErrorFields) -> io::Result<DbError> {
146145
let mut severity = None;
147146
let mut parsed_severity = None;
148147
let mut code = None;
@@ -169,8 +168,18 @@ impl DbErrorNew for DbError {
169168
b'M' => message = Some(field.value().to_owned()),
170169
b'D' => detail = Some(field.value().to_owned()),
171170
b'H' => hint = Some(field.value().to_owned()),
172-
b'P' => normal_position = Some(try!(field.value().parse::<u32>().map_err(|_| ::bad_response()))),
173-
b'p' => internal_position = Some(try!(field.value().parse::<u32>().map_err(|_| ::bad_response()))),
171+
b'P' => {
172+
normal_position = Some(try!(field.value().parse::<u32>().map_err(|_| {
173+
io::Error::new(io::ErrorKind::InvalidInput,
174+
"`P` field did not contain an integer")
175+
})));
176+
}
177+
b'p' => {
178+
internal_position = Some(try!(field.value().parse::<u32>().map_err(|_| {
179+
io::Error::new(io::ErrorKind::InvalidInput,
180+
"`p` field did not contain an integer")
181+
})));
182+
}
174183
b'q' => internal_query = Some(field.value().to_owned()),
175184
b'W' => where_ = Some(field.value().to_owned()),
176185
b's' => schema = Some(field.value().to_owned()),
@@ -179,18 +188,32 @@ impl DbErrorNew for DbError {
179188
b'd' => datatype = Some(field.value().to_owned()),
180189
b'n' => constraint = Some(field.value().to_owned()),
181190
b'F' => file = Some(field.value().to_owned()),
182-
b'L' => line = Some(try!(field.value().parse::<u32>().map_err(|_| ::bad_response()))),
191+
b'L' => {
192+
line = Some(try!(field.value().parse::<u32>().map_err(|_| {
193+
io::Error::new(io::ErrorKind::InvalidInput,
194+
"`L` field did not contain an integer")
195+
})));
196+
}
183197
b'R' => routine = Some(field.value().to_owned()),
184-
b'V' => parsed_severity = Some(try!(Severity::from_str(field.value()).ok_or_else(::bad_response))),
198+
b'V' => {
199+
parsed_severity = Some(try!(Severity::from_str(field.value()).ok_or_else(|| {
200+
io::Error::new(io::ErrorKind::InvalidInput,
201+
"`V` field contained an invalid value")
202+
})));
203+
}
185204
_ => {},
186205
}
187206
}
188207

189208
Ok(DbError {
190-
severity: try!(severity.ok_or_else(|| ::bad_response())),
209+
severity: try!(severity.ok_or_else(|| {
210+
io::Error::new(io::ErrorKind::InvalidInput, "`S` field missing")
211+
})),
191212
parsed_severity: parsed_severity,
192-
code: try!(code.ok_or_else(|| ::bad_response())),
193-
message: try!(message.ok_or_else(|| ::bad_response())),
213+
code: try!(code.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput,
214+
"`C` field missing"))),
215+
message: try!(message.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput,
216+
"`M` field missing"))),
194217
detail: detail,
195218
hint: hint,
196219
position: match normal_position {
@@ -200,7 +223,10 @@ impl DbErrorNew for DbError {
200223
Some(position) => {
201224
Some(ErrorPosition::Internal {
202225
position: position,
203-
query: try!(internal_query.ok_or_else(|| ::bad_response())),
226+
query: try!(internal_query.ok_or_else(|| {
227+
io::Error::new(io::ErrorKind::InvalidInput,
228+
"`q` field missing but `p` field present")
229+
})),
204230
})
205231
}
206232
None => None,
@@ -220,14 +246,16 @@ impl DbErrorNew for DbError {
220246
})
221247
}
222248

223-
fn new_connect<T>(fields: &mut ErrorFields) -> result::Result<T, ConnectError> {
249+
#[doc(hidden)]
250+
pub fn new_connect<T>(fields: &mut ErrorFields) -> Result<T, ConnectError> {
224251
match DbError::new_raw(fields) {
225252
Ok(err) => Err(ConnectError::Db(Box::new(err))),
226253
Err(e) => Err(ConnectError::Io(e)),
227254
}
228255
}
229256

230-
fn new<T>(fields: &mut ErrorFields) -> Result<T> {
257+
#[doc(hidden)]
258+
pub fn new<T>(fields: &mut ErrorFields) -> Result<T, Error> {
231259
match DbError::new_raw(fields) {
232260
Ok(err) => Err(Error::Db(Box::new(err))),
233261
Err(e) => Err(Error::Io(e)),

postgres-shared/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
#![allow(unknown_lints)] // for clippy
2+
13
extern crate hex;
4+
extern crate fallible_iterator;
5+
extern crate phf;
6+
extern crate postgres_protocol;
27

8+
pub mod error;
39
pub mod params;

postgres/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ bufstream = "0.1"
4040
fallible-iterator = "0.1.3"
4141
hex = "0.2"
4242
log = "0.3"
43-
phf = "=0.7.20"
4443
postgres-protocol = "0.2"
4544
bit-vec = { version = "0.4", optional = true }
4645
chrono = { version = "0.2.14", optional = true }
@@ -54,5 +53,7 @@ serde_json = { version = ">= 0.6, < 0.9", optional = true }
5453
time = { version = "0.1.14", optional = true }
5554
uuid = { version = ">= 0.1, < 0.4", optional = true }
5655

56+
postgres-shared = { path = "../postgres-shared" }
57+
5758
[dev-dependencies]
5859
url = "1.0"

postgres/src/lib.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ extern crate hex;
7676
#[cfg(not(feature = "no-logging"))]
7777
#[macro_use]
7878
extern crate log;
79-
extern crate phf;
8079
extern crate postgres_protocol;
80+
extern crate postgres_shared;
8181

8282
use fallible_iterator::{FallibleIterator, FromFallibleIterator};
8383
use std::cell::{Cell, RefCell};
@@ -90,7 +90,7 @@ use std::result;
9090
use std::sync::Arc;
9191
use std::time::Duration;
9292
use postgres_protocol::authentication;
93-
use postgres_protocol::message::backend::{self, ErrorFields};
93+
use postgres_protocol::message::backend;
9494
use postgres_protocol::message::frontend;
9595

9696
use error::{Error, ConnectError, SqlState, DbError};
@@ -103,13 +103,15 @@ use stmt::{Statement, Column};
103103
use transaction::{Transaction, IsolationLevel};
104104
use types::{IsNull, Kind, Type, SessionInfo, Oid, Other, WrongType, ToSql, FromSql, Field};
105105

106+
#[doc(inline)]
107+
pub use postgres_shared::error;
108+
106109
#[macro_use]
107110
mod macros;
108111

109112
mod feature_check;
110113
mod priv_io;
111114
mod url;
112-
pub mod error;
113115
pub mod tls;
114116
pub mod notification;
115117
pub mod params;
@@ -473,7 +475,7 @@ impl InnerConnection {
473475
.map_err(Into::into)
474476
.and_then(|oid| self.get_type(oid))
475477
.collect());
476-
478+
477479
let columns = match raw_columns {
478480
Some(body) => {
479481
try!(body.fields()
@@ -1373,12 +1375,6 @@ trait OtherNew {
13731375
fn new(name: String, oid: Oid, kind: Kind, schema: String) -> Other;
13741376
}
13751377

1376-
trait DbErrorNew {
1377-
fn new_raw(fields: &mut ErrorFields) -> io::Result<DbError>;
1378-
fn new_connect<T>(fields: &mut ErrorFields) -> result::Result<T, ConnectError>;
1379-
fn new<T>(fields: &mut ErrorFields) -> Result<T>;
1380-
}
1381-
13821378
trait RowsNew<'a> {
13831379
fn new(stmt: &'a Statement<'a>, data: Vec<RowData>) -> Rows<'a>;
13841380
fn new_owned(stmt: Statement<'a>, data: Vec<RowData>) -> Rows<'a>;

postgres/src/stmt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use types::{SessionInfo, Type, ToSql};
1313
use rows::{Rows, LazyRows};
1414
use transaction::Transaction;
1515
use {bad_response, Connection, StatementInternals, Result, RowsNew, InnerConnection, RowData,
16-
SessionInfoNew, LazyRowsNew, DbErrorNew, ColumnNew, StatementInfo, TransactionInternals};
16+
SessionInfoNew, LazyRowsNew, ColumnNew, StatementInfo, TransactionInternals};
1717

1818
/// A prepared statement.
1919
pub struct Statement<'conn> {

0 commit comments

Comments
 (0)