1 /*
2  * Copyright (C) 2010, Nokia <ivan.frade@nokia.com>
3  * Copyright (C) 2020, Sam Thursfield <sam@afuera.me.uk>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA  02110-1301, USA.
19  */
20 
21 #include "tracker-error.h"
22 #include <libtracker-data/tracker-data.h>
23 
24 static const GDBusErrorEntry tracker_sparql_error_entries[] =
25 {
26 	{TRACKER_SPARQL_ERROR_CONSTRAINT, "org.freedesktop.Tracker.Error.Constraint"},
27 	{TRACKER_SPARQL_ERROR_INTERNAL, "org.freedesktop.Tracker.Error.Internal"},
28 	{TRACKER_SPARQL_ERROR_NO_SPACE, "org.freedesktop.Tracker.Error.NoSpace"},
29 	{TRACKER_SPARQL_ERROR_ONTOLOGY_NOT_FOUND, "org.freedesktop.Tracker.Error.OntologyNotFound"},
30 	{TRACKER_SPARQL_ERROR_OPEN_ERROR, "org.freedesktop.Tracker.Error.OpenError"},
31 	{TRACKER_SPARQL_ERROR_PARSE, "org.freedesktop.Tracker.Error.Parse"},
32 	{TRACKER_SPARQL_ERROR_QUERY_FAILED, "org.freedesktop.Tracker.Error.QueryFailed"},
33 	{TRACKER_SPARQL_ERROR_TYPE, "org.freedesktop.Tracker.Error.Type"},
34 	{TRACKER_SPARQL_ERROR_UNKNOWN_CLASS, "org.freedesktop.Tracker.Error.UnknownClass"},
35 	{TRACKER_SPARQL_ERROR_UNKNOWN_GRAPH, "org.freedesktop.Tracker.Error.UnknownGraph"},
36 	{TRACKER_SPARQL_ERROR_UNKNOWN_PROPERTY, "org.freedesktop.Tracker.Error.UnknownProperty"},
37 	{TRACKER_SPARQL_ERROR_UNSUPPORTED, "org.freedesktop.Tracker.Error.Unsupported"},
38 };
39 
40 G_STATIC_ASSERT (G_N_ELEMENTS (tracker_sparql_error_entries) == TRACKER_SPARQL_N_ERRORS);
41 
42 GQuark
tracker_sparql_error_quark(void)43 tracker_sparql_error_quark (void)
44 {
45        static volatile gsize quark_volatile = 0;
46        g_dbus_error_register_error_domain ("tracker-sparql-error-quark",
47                                            &quark_volatile,
48                                            tracker_sparql_error_entries,
49                                            G_N_ELEMENTS (tracker_sparql_error_entries));
50        return (GQuark) quark_volatile;
51 }
52 
53 /* Converts internal error codes from libtracker-data into public
54  * TrackerSparqlError codes. */
55 GError *
_translate_internal_error(GError * error)56 _translate_internal_error (GError *error)
57 {
58 	GError *new_error = NULL;
59 
60 	if (error->domain == TRACKER_DATA_ONTOLOGY_ERROR) {
61 		switch (error->code) {
62 			case TRACKER_DATA_ONTOLOGY_NOT_FOUND:
63 				new_error = g_error_new_literal (TRACKER_SPARQL_ERROR,
64 				                                 TRACKER_SPARQL_ERROR_ONTOLOGY_NOT_FOUND,
65 				                                 error->message);
66 				break;
67 			case TRACKER_DATA_UNSUPPORTED_LOCATION:
68 			case TRACKER_DATA_UNSUPPORTED_ONTOLOGY_CHANGE:
69 				new_error = g_error_new_literal (TRACKER_SPARQL_ERROR,
70 				                                 TRACKER_SPARQL_ERROR_UNSUPPORTED,
71 				                                 error->message);
72 				break;
73 			default:
74 				new_error = g_error_new_literal (TRACKER_SPARQL_ERROR,
75 				                                 TRACKER_SPARQL_ERROR_INTERNAL,
76 				                                 error->message);
77 		}
78 	} else if (error->domain == TRACKER_DB_INTERFACE_ERROR) {
79 		TrackerSparqlError new_code = TRACKER_SPARQL_ERROR_INTERNAL;
80 
81 		switch (error->code) {
82 			case TRACKER_DB_QUERY_ERROR: new_code = TRACKER_SPARQL_ERROR_QUERY_FAILED; break;
83 			case TRACKER_DB_OPEN_ERROR: new_code = TRACKER_SPARQL_ERROR_OPEN_ERROR; break;
84 			case TRACKER_DB_NO_SPACE: new_code = TRACKER_SPARQL_ERROR_NO_SPACE; break;
85 			/* This should never happen as we don't call sqlite3_interrupt()
86 			 * anywhere, so it doesn't get its own public error code. */
87 			case TRACKER_DB_INTERRUPTED: new_code = TRACKER_SPARQL_ERROR_INTERNAL; break;
88 			case TRACKER_DB_CONSTRAINT: new_code = TRACKER_SPARQL_ERROR_CONSTRAINT; break;
89 			default: g_warn_if_reached ();
90 		}
91 
92 		new_error = g_error_new_literal (TRACKER_SPARQL_ERROR, new_code, error->message);
93 	}
94 
95 	if (new_error) {
96 		g_error_free (error);
97 		return new_error;
98 	} else {
99 		return error;
100 	}
101 }
102