Skip to content

Commit a05adff

Browse files
committed
Make DbError's fields private
1 parent 1d9ae82 commit a05adff

File tree

1 file changed

+129
-98
lines changed
  • tokio-postgres/src/error

1 file changed

+129
-98
lines changed

tokio-postgres/src/error/mod.rs

+129-98
Original file line numberDiff line numberDiff line change
@@ -65,83 +65,28 @@ impl Severity {
6565
}
6666

6767
/// A Postgres error or notice.
68-
#[derive(Clone, PartialEq, Eq)]
68+
#[derive(Debug, Clone, PartialEq, Eq)]
6969
pub struct DbError {
70-
/// The field contents are ERROR, FATAL, or PANIC (in an error message),
71-
/// or WARNING, NOTICE, DEBUG, INFO, or LOG (in a notice message), or a
72-
/// localized translation of one of these.
73-
pub severity: String,
74-
75-
/// A parsed, nonlocalized version of `severity`. (PostgreSQL 9.6+)
76-
pub parsed_severity: Option<Severity>,
77-
78-
/// The SQLSTATE code for the error.
79-
pub code: SqlState,
80-
81-
/// The primary human-readable error message. This should be accurate but
82-
/// terse (typically one line).
83-
pub message: String,
84-
85-
/// An optional secondary error message carrying more detail about the
86-
/// problem. Might run to multiple lines.
87-
pub detail: Option<String>,
88-
89-
/// An optional suggestion what to do about the problem. This is intended
90-
/// to differ from Detail in that it offers advice (potentially
91-
/// inappropriate) rather than hard facts. Might run to multiple lines.
92-
pub hint: Option<String>,
93-
94-
/// An optional error cursor position into either the original query string
95-
/// or an internally generated query.
96-
pub position: Option<ErrorPosition>,
97-
98-
/// An indication of the context in which the error occurred. Presently
99-
/// this includes a call stack traceback of active procedural language
100-
/// functions and internally-generated queries. The trace is one entry per
101-
/// line, most recent first.
102-
pub where_: Option<String>,
103-
104-
/// If the error was associated with a specific database object, the name
105-
/// of the schema containing that object, if any. (PostgreSQL 9.3+)
106-
pub schema: Option<String>,
107-
108-
/// If the error was associated with a specific table, the name of the
109-
/// table. (Refer to the schema name field for the name of the table's
110-
/// schema.) (PostgreSQL 9.3+)
111-
pub table: Option<String>,
112-
113-
/// If the error was associated with a specific table column, the name of
114-
/// the column. (Refer to the schema and table name fields to identify the
115-
/// table.) (PostgreSQL 9.3+)
116-
pub column: Option<String>,
117-
118-
/// If the error was associated with a specific data type, the name of the
119-
/// data type. (Refer to the schema name field for the name of the data
120-
/// type's schema.) (PostgreSQL 9.3+)
121-
pub datatype: Option<String>,
122-
123-
/// If the error was associated with a specific constraint, the name of the
124-
/// constraint. Refer to fields listed above for the associated table or
125-
/// domain. (For this purpose, indexes are treated as constraints, even if
126-
/// they weren't created with constraint syntax.) (PostgreSQL 9.3+)
127-
pub constraint: Option<String>,
128-
129-
/// The file name of the source-code location where the error was reported.
130-
pub file: Option<String>,
131-
132-
/// The line number of the source-code location where the error was
133-
/// reported.
134-
pub line: Option<u32>,
135-
136-
/// The name of the source-code routine reporting the error.
137-
pub routine: Option<String>,
138-
139-
_p: (),
70+
severity: String,
71+
parsed_severity: Option<Severity>,
72+
code: SqlState,
73+
message: String,
74+
detail: Option<String>,
75+
hint: Option<String>,
76+
position: Option<ErrorPosition>,
77+
where_: Option<String>,
78+
schema: Option<String>,
79+
table: Option<String>,
80+
column: Option<String>,
81+
datatype: Option<String>,
82+
constraint: Option<String>,
83+
file: Option<String>,
84+
line: Option<u32>,
85+
routine: Option<String>,
14086
}
14187

14288
impl DbError {
143-
#[doc(hidden)]
144-
pub fn new(fields: &mut ErrorFields) -> io::Result<DbError> {
89+
pub(crate) fn new(fields: &mut ErrorFields) -> io::Result<DbError> {
14590
let mut severity = None;
14691
let mut parsed_severity = None;
14792
let mut code = None;
@@ -224,7 +169,7 @@ impl DbError {
224169
detail: detail,
225170
hint: hint,
226171
position: match normal_position {
227-
Some(position) => Some(ErrorPosition::Normal(position)),
172+
Some(position) => Some(ErrorPosition::Original(position)),
228173
None => match internal_position {
229174
Some(position) => Some(ErrorPosition::Internal {
230175
position: position,
@@ -247,32 +192,118 @@ impl DbError {
247192
file: file,
248193
line: line,
249194
routine: routine,
250-
_p: (),
251195
})
252196
}
253-
}
254197

255-
// manual impl to leave out _p
256-
impl fmt::Debug for DbError {
257-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
258-
fmt.debug_struct("DbError")
259-
.field("severity", &self.severity)
260-
.field("parsed_severity", &self.parsed_severity)
261-
.field("code", &self.code)
262-
.field("message", &self.message)
263-
.field("detail", &self.detail)
264-
.field("hint", &self.hint)
265-
.field("position", &self.position)
266-
.field("where_", &self.where_)
267-
.field("schema", &self.schema)
268-
.field("table", &self.table)
269-
.field("column", &self.column)
270-
.field("datatype", &self.datatype)
271-
.field("constraint", &self.constraint)
272-
.field("file", &self.file)
273-
.field("line", &self.line)
274-
.field("routine", &self.routine)
275-
.finish()
198+
/// The field contents are ERROR, FATAL, or PANIC (in an error message),
199+
/// or WARNING, NOTICE, DEBUG, INFO, or LOG (in a notice message), or a
200+
/// localized translation of one of these.
201+
pub fn severity(&self) -> &str {
202+
&self.severity
203+
}
204+
205+
/// A parsed, nonlocalized version of `severity`. (PostgreSQL 9.6+)
206+
pub fn parsed_severity(&self) -> Option<Severity> {
207+
self.parsed_severity
208+
}
209+
210+
/// The SQLSTATE code for the error.
211+
pub fn code(&self) -> &SqlState {
212+
&self.code
213+
}
214+
215+
/// The primary human-readable error message.
216+
///
217+
/// This should be accurate but terse (typically one line).
218+
pub fn message(&self) -> &str {
219+
&self.message
220+
}
221+
222+
/// An optional secondary error message carrying more detail about the
223+
/// problem.
224+
///
225+
/// Might run to multiple lines.
226+
pub fn detail(&self) -> Option<&str> {
227+
self.detail.as_ref().map(|s| &**s)
228+
}
229+
230+
/// An optional suggestion what to do about the problem.
231+
///
232+
/// This is intended to differ from `detail` in that it offers advice
233+
/// (potentially inappropriate) rather than hard facts. Might run to
234+
/// multiple lines.
235+
pub fn hint(&self) -> Option<&str> {
236+
self.hint.as_ref().map(|s| &**s)
237+
}
238+
239+
/// An optional error cursor position into either the original query string
240+
/// or an internally generated query.
241+
pub fn position(&self) -> Option<&ErrorPosition> {
242+
self.position.as_ref()
243+
}
244+
245+
/// An indication of the context in which the error occurred.
246+
///
247+
/// Presently this includes a call stack traceback of active procedural
248+
/// language functions and internally-generated queries. The trace is one
249+
/// entry per line, most recent first.
250+
pub fn where_(&self) -> Option<&str> {
251+
self.where_.as_ref().map(|s| &**s)
252+
}
253+
254+
/// If the error was associated with a specific database object, the name
255+
/// of the schema containing that object, if any. (PostgreSQL 9.3+)
256+
pub fn schema(&self) -> Option<&str> {
257+
self.schema.as_ref().map(|s| &**s)
258+
}
259+
260+
/// If the error was associated with a specific table, the name of the
261+
/// table. (Refer to the schema name field for the name of the table's
262+
/// schema.) (PostgreSQL 9.3+)
263+
pub fn table(&self) -> Option<&str> {
264+
self.table.as_ref().map(|s| &**s)
265+
}
266+
267+
/// If the error was associated with a specific table column, the name of
268+
/// the column.
269+
///
270+
/// (Refer to the schema and table name fields to identify the table.)
271+
/// (PostgreSQL 9.3+)
272+
pub fn column(&self) -> Option<&str> {
273+
self.column.as_ref().map(|s| &**s)
274+
}
275+
276+
/// If the error was associated with a specific data type, the name of the
277+
/// data type. (Refer to the schema name field for the name of the data
278+
/// type's schema.) (PostgreSQL 9.3+)
279+
pub fn datatype(&self) -> Option<&str> {
280+
self.datatype.as_ref().map(|s| &**s)
281+
}
282+
283+
/// If the error was associated with a specific constraint, the name of the
284+
/// constraint.
285+
///
286+
/// Refer to fields listed above for the associated table or domain.
287+
/// (For this purpose, indexes are treated as constraints, even if they
288+
/// weren't created with constraint syntax.) (PostgreSQL 9.3+)
289+
pub fn constraint(&self) -> Option<&str> {
290+
self.constraint.as_ref().map(|s| &**s)
291+
}
292+
293+
/// The file name of the source-code location where the error was reported.
294+
pub fn file(&self) -> Option<&str> {
295+
self.file.as_ref().map(|s| &**s)
296+
}
297+
298+
/// The line number of the source-code location where the error was
299+
/// reported.
300+
pub fn line(&self) -> Option<u32> {
301+
self.line
302+
}
303+
304+
/// The name of the source-code routine reporting the error.
305+
pub fn routine(&self) -> Option<&str> {
306+
self.routine.as_ref().map(|s| &**s)
276307
}
277308
}
278309

@@ -292,7 +323,7 @@ impl error::Error for DbError {
292323
#[derive(Clone, PartialEq, Eq, Debug)]
293324
pub enum ErrorPosition {
294325
/// A position in the original query.
295-
Normal(u32),
326+
Original(u32),
296327
/// A position in an internally generated query.
297328
Internal {
298329
/// The byte position.

0 commit comments

Comments
 (0)