Mercurial > gemma
annotate pkg/imports/errors.go @ 3334:8c96b7379aea
client: fairway availability: made store action work with both types of feature objects
author | Markus Kottlaender <markus@intevation.de> |
---|---|
date | Mon, 20 May 2019 17:14:47 +0200 |
parents | e0dabe7b2fcf |
children | 8e083b271fca |
rev | line source |
---|---|
2548
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
1 // This is Free Software under GNU Affero General Public License v >= 3.0 |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
2 // without warranty, see README.md and license for details. |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
3 // |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
4 // SPDX-License-Identifier: AGPL-3.0-or-later |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
5 // License-Filename: LICENSES/AGPL-3.0.txt |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
6 // |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
7 // Copyright (C) 2019 by via donau |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
8 // – Österreichische Wasserstraßen-Gesellschaft mbH |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
9 // Software engineering by Intevation GmbH |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
10 // |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
11 // Author(s): |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
12 // * Tom Gottfried <tom.gottfried@intevation.de> |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
13 |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
14 package imports |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
15 |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
16 import ( |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
17 "strings" |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
18 |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
19 "github.com/jackc/pgx" |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
20 ) |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
21 |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
22 func handleError(err error) error { |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
23 switch e := err.(type) { |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
24 case pgx.PgError: |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
25 return dbError(e) |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
26 } |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
27 return err |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
28 } |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
29 |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
30 // Handle PostgreSQL error codes |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
31 const ( |
3310 | 32 notNullViolation = "23502" |
3176
1cb6676d1510
Handle failing INSERTs gracefully during gauge measurements import
Tom Gottfried <tom@intevation.de>
parents:
2548
diff
changeset
|
33 foreignKeyViolation = "23503" |
1cb6676d1510
Handle failing INSERTs gracefully during gauge measurements import
Tom Gottfried <tom@intevation.de>
parents:
2548
diff
changeset
|
34 noDataFound = "P0002" |
2548
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
35 ) |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
36 |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
37 type dbError pgx.PgError |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
38 |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
39 func (err dbError) Error() string { |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
40 switch err.Code { |
3310 | 41 case notNullViolation: |
42 switch err.SchemaName { | |
43 case "waterway": | |
44 switch err.TableName { | |
45 case "gauges": | |
46 switch err.ColumnName { | |
47 case "objname": | |
48 return "Missing objname" | |
49 case "geom": | |
50 return "Missing lat/lon" | |
51 case "zero_point": | |
52 return "Missing zeropoint" | |
53 } | |
54 } | |
55 } | |
3176
1cb6676d1510
Handle failing INSERTs gracefully during gauge measurements import
Tom Gottfried <tom@intevation.de>
parents:
2548
diff
changeset
|
56 case foreignKeyViolation: |
1cb6676d1510
Handle failing INSERTs gracefully during gauge measurements import
Tom Gottfried <tom@intevation.de>
parents:
2548
diff
changeset
|
57 switch err.SchemaName { |
1cb6676d1510
Handle failing INSERTs gracefully during gauge measurements import
Tom Gottfried <tom@intevation.de>
parents:
2548
diff
changeset
|
58 case "waterway": |
1cb6676d1510
Handle failing INSERTs gracefully during gauge measurements import
Tom Gottfried <tom@intevation.de>
parents:
2548
diff
changeset
|
59 switch err.TableName { |
1cb6676d1510
Handle failing INSERTs gracefully during gauge measurements import
Tom Gottfried <tom@intevation.de>
parents:
2548
diff
changeset
|
60 case "gauge_measurements": |
1cb6676d1510
Handle failing INSERTs gracefully during gauge measurements import
Tom Gottfried <tom@intevation.de>
parents:
2548
diff
changeset
|
61 switch err.ConstraintName { |
1cb6676d1510
Handle failing INSERTs gracefully during gauge measurements import
Tom Gottfried <tom@intevation.de>
parents:
2548
diff
changeset
|
62 case "gauge_key": |
1cb6676d1510
Handle failing INSERTs gracefully during gauge measurements import
Tom Gottfried <tom@intevation.de>
parents:
2548
diff
changeset
|
63 return "Referenced gauge is not in database" |
1cb6676d1510
Handle failing INSERTs gracefully during gauge measurements import
Tom Gottfried <tom@intevation.de>
parents:
2548
diff
changeset
|
64 } |
1cb6676d1510
Handle failing INSERTs gracefully during gauge measurements import
Tom Gottfried <tom@intevation.de>
parents:
2548
diff
changeset
|
65 } |
1cb6676d1510
Handle failing INSERTs gracefully during gauge measurements import
Tom Gottfried <tom@intevation.de>
parents:
2548
diff
changeset
|
66 } |
2548
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
67 case noDataFound: |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
68 // Most recent line from stacktrace contains name of failed function |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
69 recent := strings.SplitN(err.Where, "\n", 1)[0] |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
70 switch { |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
71 case strings.Contains(recent, "isrsrange_points"): |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
72 return "No distance mark found for at least one given ISRS Location Code" |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
73 case strings.Contains(recent, "isrsrange_axis"): |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
74 return "No contiguous axis found between given ISRS Location Codes" |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
75 } |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
76 } |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
77 return "Unexpected database error: " + err.Message |
6b34d0fb4498
Improve user facing error messages for stretch area generation
Tom Gottfried <tom@intevation.de>
parents:
diff
changeset
|
78 } |