1 /*
2     SPDX-FileCopyrightText: 2016 Sandro Knauß <sknauss@kde.org>
3 
4     SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "daverror.h"
8 #include <KLocalizedString>
9 
10 #include <KIO/Global>
11 #include <KIO/Job>
12 
13 using namespace KDAV;
14 
15 namespace KDAV
16 {
17 class ErrorPrivate : public QSharedData
18 {
19 public:
20     ErrorNumber mErrorNumber = NO_ERR;
21     int mResponseCode = 0;
22     int mJobErrorCode = 0;
23     QString mErrorText;
24 };
25 }
26 
Error()27 Error::Error()
28     : d(new ErrorPrivate)
29 {
30 }
31 
Error(ErrorNumber errNo,int responseCode,const QString & errorText,int jobErrorCode)32 Error::Error(ErrorNumber errNo, int responseCode, const QString &errorText, int jobErrorCode)
33     : d(new ErrorPrivate)
34 {
35     d->mErrorNumber = errNo;
36     d->mResponseCode = responseCode;
37     d->mErrorText = errorText;
38     d->mJobErrorCode = jobErrorCode;
39 }
40 
41 Error::Error(const Error &) = default;
42 Error::Error(Error &&) = default;
43 Error::~Error() = default;
44 Error &Error::operator=(const Error &) = default;
45 Error &Error::operator=(Error &&) = default;
46 
errorNumber() const47 ErrorNumber Error::errorNumber() const
48 {
49     return d->mErrorNumber;
50 }
51 
internalErrorText() const52 QString Error::internalErrorText() const
53 {
54     return d->mErrorText;
55 }
56 
jobErrorCode() const57 int Error::jobErrorCode() const
58 {
59     return d->mJobErrorCode;
60 }
61 
responseCode() const62 int Error::responseCode() const
63 {
64     return d->mResponseCode;
65 }
66 
translatedJobError() const67 QString KDAV::Error::translatedJobError() const
68 {
69     QString err;
70     if (d->mJobErrorCode > 0 && d->mJobErrorCode != KIO::ERR_SLAVE_DEFINED) {
71         err = KIO::buildErrorString(d->mJobErrorCode, d->mErrorText);
72     } else {
73         err = d->mErrorText;
74     }
75     return err;
76 }
77 
errorText() const78 QString Error::errorText() const
79 {
80     QString result;
81 
82     QString err = translatedJobError();
83 
84     switch (d->mErrorNumber) {
85     case ERR_PROBLEM_WITH_REQUEST:
86         // User-side error
87         if (d->mResponseCode == 401) {
88             err = i18n("Invalid username/password");
89         } else if (d->mResponseCode == 403) {
90             err = i18n("Access forbidden");
91         } else if (d->mResponseCode == 404) {
92             err = i18n("Resource not found");
93         } else {
94             err = i18n("HTTP error");
95         }
96         result = i18n("There was a problem with the request.\n%1 (%2).", err, d->mResponseCode);
97         break;
98     case ERR_NO_MULTIGET:
99         result = i18n("Protocol for the collection does not support MULTIGET");
100         break;
101     case ERR_SERVER_UNRECOVERABLE:
102         result = i18n("The server encountered an error that prevented it from completing your request: %1 (%2)", err, d->mResponseCode);
103         break;
104     case ERR_COLLECTIONDELETE:
105         result = i18n("There was a problem with the request. The collection has not been deleted from the server.\n%1 (%2).", err, d->mResponseCode);
106         break;
107     case ERR_COLLECTIONFETCH:
108         result = i18n("Invalid responses from backend");
109         break;
110     case ERR_COLLECTIONFETCH_XQUERY_SETFOCUS:
111         result = i18n("Error setting focus for XQuery");
112         break;
113     case ERR_COLLECTIONFETCH_XQUERY_INVALID:
114         result = i18n("Invalid XQuery submitted by DAV implementation");
115         break;
116     case ERR_COLLECTIONMODIFY:
117         result = i18n(
118             "There was a problem with the request. The collection has not been modified on the server.\n"
119             "%1 (%2).",
120             err,
121             d->mResponseCode);
122         break;
123     case ERR_COLLECTIONMODIFY_NO_PROPERITES:
124         result = i18n("No properties to change or remove");
125         break;
126     case ERR_COLLECTIONMODIFY_RESPONSE:
127         result = i18n("There was an error when modifying the properties");
128         if (!d->mErrorText.isEmpty()) {
129             result.append(i18n("\nThe server returned more information:\n%1", d->mErrorText));
130         }
131         break;
132     case ERR_ITEMCREATE:
133         result = i18n("There was a problem with the request. The item has not been created on the server.\n%1 (%2).", err, d->mResponseCode);
134         break;
135     case ERR_ITEMDELETE:
136         result = i18n("There was a problem with the request. The item has not been deleted from the server.\n%1 (%2).", err, d->mResponseCode);
137         break;
138     case ERR_ITEMMODIFY:
139         result = i18n("There was a problem with the request. The item was not modified on the server.\n%1 (%2).", err, d->mResponseCode);
140         break;
141     case ERR_ITEMLIST: {
142         result = i18n("There was a problem with the request.");
143         break;
144     };
145     case ERR_ITEMLIST_NOMIMETYPE:
146         result = i18n("There was a problem with the request. The requested MIME types are not supported.");
147         break;
148     case NO_ERR:
149         break;
150     }
151     return result;
152 }
153