Add human readable version of errors
This commit is contained in:
parent
753a950816
commit
68131a6e2a
5 changed files with 55 additions and 42 deletions
26
js/api.js
26
js/api.js
|
@ -85,32 +85,28 @@ window.textsecure.api = function() {
|
|||
}
|
||||
if (code > 999 || code < 100)
|
||||
code = -1;
|
||||
var e = new Error(code);
|
||||
e.name = "HTTPError";
|
||||
try {
|
||||
switch (code) {
|
||||
case -1:
|
||||
e.human_error = "Failed to connect to the server, please check your network connection.";
|
||||
break;
|
||||
textsecure.throwHumanError(code, "HTTPError", "Failed to connect to the server, please check your network connection.");
|
||||
case 413:
|
||||
e.human_error = "Rate limit exceeded, please try again later.";
|
||||
break;
|
||||
textsecure.throwHumanError(code, "HTTPError", "Rate limit exceeded, please try again later.");
|
||||
case 403:
|
||||
e.human_error = "Invalid code, please try again.";
|
||||
break;
|
||||
textsecure.throwHumanError(code, "HTTPError", "Invalid code, please try again.");
|
||||
case 417:
|
||||
e.human_error = "Number already registered"; // TODO: This shouldn't be a thing?, but its in the API doc?
|
||||
break;
|
||||
// TODO: This shouldn't be a thing?, but its in the API doc?
|
||||
textsecure.throwHumanError(code, "HTTPError", "Number already registered.");
|
||||
case 401:
|
||||
e.human_error = "Invalid authentication, most likely someone re-registered and invalidated our registration";
|
||||
break;
|
||||
textsecure.throwHumanError(code, "HTTPError", "Invalid authentication, most likely someone re-registered and invalidated our registration.");
|
||||
case 404:
|
||||
e.human_error = "Number is not registered with TextSecure..";
|
||||
break;
|
||||
textsecure.throwHumanError(code, "HTTPError", "Number is not registered with TextSecure.");
|
||||
default:
|
||||
e.human_error = "The server rejected our query, please file a bug report.";
|
||||
textsecure.throwHumanError(code, "HTTPError", "The server rejected our query, please file a bug report.");
|
||||
}
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
|
@ -417,10 +417,7 @@ window.textsecure.crypto = new function() {
|
|||
} else {
|
||||
// ...otherwise create an error that the UI will pick up and ask the user if they want to re-negotiate
|
||||
// TODO: Save the message for possible later renegotiation
|
||||
var error = new Error("Received message with unknown identity key");
|
||||
error.name = "WarnTryAgainError";
|
||||
error.full_message = "The identity of the sender has changed. This may be malicious, or the sender may have simply reinstalled TextSecure.";
|
||||
throw new error;
|
||||
textsecure.throwHumanError("Received message with unknown identity key", "WarnTryAgainError", "The identity of the sender has changed. This may be malicious, or the sender may have simply reinstalled TextSecure.");
|
||||
}
|
||||
}
|
||||
return initSession(false, preKeyPair, encodedNumber, toArrayBuffer(message.identityKey), toArrayBuffer(message.baseKey))
|
||||
|
|
|
@ -277,6 +277,14 @@ window.textsecure.utils = function() {
|
|||
return self;
|
||||
}();
|
||||
|
||||
window.textsecure.throwHumanError = function(error, type, humanError) {
|
||||
var e = new Error(error);
|
||||
if (type !== undefined)
|
||||
e.name = type;
|
||||
e.humanError = humanError;
|
||||
throw e;
|
||||
}
|
||||
|
||||
/************************************************
|
||||
*** Utilities to store data in local storage ***
|
||||
************************************************/
|
||||
|
@ -500,6 +508,7 @@ window.textsecure.subscribeToPush = function() {
|
|||
});
|
||||
})
|
||||
}).catch(function(e) {
|
||||
// TODO: Show "Invalid message" messages?
|
||||
console.log("Error handling incoming message: ");
|
||||
console.log(e);
|
||||
});
|
||||
|
@ -533,6 +542,19 @@ window.textsecure.sendMessage = function() {
|
|||
var promises = [];
|
||||
|
||||
var addEncryptionFor = function(i) {
|
||||
return new Promise(function(resolve) { // Wrap in a promise for the throws
|
||||
if (deviceObjectList[i].relay !== undefined) {
|
||||
if (relay === undefined)
|
||||
relay = deviceObjectList[i].relay;
|
||||
else if (relay != deviceObjectList[i].relay)
|
||||
throw new Error("Mismatched relays for number " + number);
|
||||
} else {
|
||||
if (relay === undefined)
|
||||
relay = "";
|
||||
else if (relay != "")
|
||||
throw new Error("Mismatched relays for number " + number);
|
||||
}
|
||||
|
||||
return textsecure.crypto.encryptMessageFor(deviceObjectList[i], message).then(function(encryptedMsg) {
|
||||
jsonData[i] = {
|
||||
type: encryptedMsg.type,
|
||||
|
@ -542,20 +564,12 @@ window.textsecure.sendMessage = function() {
|
|||
timestamp: new Date().getTime()
|
||||
};
|
||||
|
||||
if (deviceObjectList[i].relay !== undefined) {
|
||||
if (deviceObjectList[i].relay !== undefined)
|
||||
jsonData[i].relay = deviceObjectList[i].relay;
|
||||
if (relay === undefined)
|
||||
relay = jsonData[i].relay;
|
||||
else if (relay != jsonData[i].relay)
|
||||
throw new Error("Mismatched relays for number " + number);
|
||||
} else {
|
||||
if (relay === undefined)
|
||||
relay = "";
|
||||
else if (relay != "")
|
||||
throw new Error("Mismatched relays for number " + number);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
for (var i = 0; i < deviceObjectList.length; i++)
|
||||
promises[i] = addEncryptionFor(i);
|
||||
return Promise.all(promises).then(function() {
|
||||
|
@ -575,6 +589,8 @@ window.textsecure.sendMessage = function() {
|
|||
}
|
||||
|
||||
var registerError = function(number, message, error) {
|
||||
if (error.humanError)
|
||||
message = error.humanError;
|
||||
errors[errors.length] = { number: number, reason: message, error: error };
|
||||
numberCompleted();
|
||||
}
|
||||
|
|
|
@ -100,7 +100,11 @@ $('#init-go').click(function() {
|
|||
registrationDone();
|
||||
}
|
||||
}).catch(function(error) {
|
||||
alert(error.human_error);
|
||||
//TODO: No alerts...
|
||||
if (error.humanError)
|
||||
alert(error.humanError);
|
||||
else
|
||||
alert(error); //XXX
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ window.crypto.subtle = (function() {
|
|||
var StaticArrayBufferProto = new ArrayBuffer().__proto__;
|
||||
function assertIsArrayBuffer(thing) {
|
||||
if (thing !== Object(thing) || thing.__proto__ != StaticArrayBufferProto)
|
||||
throw new Error("WARNING: Needed a ArrayBuffer");
|
||||
throw new Error("Needed a ArrayBuffer");
|
||||
}
|
||||
|
||||
// private implementation functions
|
||||
|
|
Loading…
Reference in a new issue