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)
|
if (code > 999 || code < 100)
|
||||||
code = -1;
|
code = -1;
|
||||||
var e = new Error(code);
|
try {
|
||||||
e.name = "HTTPError";
|
|
||||||
switch (code) {
|
switch (code) {
|
||||||
case -1:
|
case -1:
|
||||||
e.human_error = "Failed to connect to the server, please check your network connection.";
|
textsecure.throwHumanError(code, "HTTPError", "Failed to connect to the server, please check your network connection.");
|
||||||
break;
|
|
||||||
case 413:
|
case 413:
|
||||||
e.human_error = "Rate limit exceeded, please try again later.";
|
textsecure.throwHumanError(code, "HTTPError", "Rate limit exceeded, please try again later.");
|
||||||
break;
|
|
||||||
case 403:
|
case 403:
|
||||||
e.human_error = "Invalid code, please try again.";
|
textsecure.throwHumanError(code, "HTTPError", "Invalid code, please try again.");
|
||||||
break;
|
|
||||||
case 417:
|
case 417:
|
||||||
e.human_error = "Number already registered"; // TODO: This shouldn't be a thing?, but its in the API doc?
|
// TODO: This shouldn't be a thing?, but its in the API doc?
|
||||||
break;
|
textsecure.throwHumanError(code, "HTTPError", "Number already registered.");
|
||||||
case 401:
|
case 401:
|
||||||
e.human_error = "Invalid authentication, most likely someone re-registered and invalidated our registration";
|
textsecure.throwHumanError(code, "HTTPError", "Invalid authentication, most likely someone re-registered and invalidated our registration.");
|
||||||
break;
|
|
||||||
case 404:
|
case 404:
|
||||||
e.human_error = "Number is not registered with TextSecure..";
|
textsecure.throwHumanError(code, "HTTPError", "Number is not registered with TextSecure.");
|
||||||
break;
|
|
||||||
default:
|
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);
|
reject(e);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
@ -417,10 +417,7 @@ window.textsecure.crypto = new function() {
|
||||||
} else {
|
} else {
|
||||||
// ...otherwise create an error that the UI will pick up and ask the user if they want to re-negotiate
|
// ...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
|
// TODO: Save the message for possible later renegotiation
|
||||||
var error = new Error("Received message with unknown identity key");
|
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.");
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return initSession(false, preKeyPair, encodedNumber, toArrayBuffer(message.identityKey), toArrayBuffer(message.baseKey))
|
return initSession(false, preKeyPair, encodedNumber, toArrayBuffer(message.identityKey), toArrayBuffer(message.baseKey))
|
||||||
|
|
|
@ -277,6 +277,14 @@ window.textsecure.utils = function() {
|
||||||
return self;
|
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 ***
|
*** Utilities to store data in local storage ***
|
||||||
************************************************/
|
************************************************/
|
||||||
|
@ -500,6 +508,7 @@ window.textsecure.subscribeToPush = function() {
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
}).catch(function(e) {
|
}).catch(function(e) {
|
||||||
|
// TODO: Show "Invalid message" messages?
|
||||||
console.log("Error handling incoming message: ");
|
console.log("Error handling incoming message: ");
|
||||||
console.log(e);
|
console.log(e);
|
||||||
});
|
});
|
||||||
|
@ -533,6 +542,19 @@ window.textsecure.sendMessage = function() {
|
||||||
var promises = [];
|
var promises = [];
|
||||||
|
|
||||||
var addEncryptionFor = function(i) {
|
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) {
|
return textsecure.crypto.encryptMessageFor(deviceObjectList[i], message).then(function(encryptedMsg) {
|
||||||
jsonData[i] = {
|
jsonData[i] = {
|
||||||
type: encryptedMsg.type,
|
type: encryptedMsg.type,
|
||||||
|
@ -542,20 +564,12 @@ window.textsecure.sendMessage = function() {
|
||||||
timestamp: new Date().getTime()
|
timestamp: new Date().getTime()
|
||||||
};
|
};
|
||||||
|
|
||||||
if (deviceObjectList[i].relay !== undefined) {
|
if (deviceObjectList[i].relay !== undefined)
|
||||||
jsonData[i].relay = deviceObjectList[i].relay;
|
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++)
|
for (var i = 0; i < deviceObjectList.length; i++)
|
||||||
promises[i] = addEncryptionFor(i);
|
promises[i] = addEncryptionFor(i);
|
||||||
return Promise.all(promises).then(function() {
|
return Promise.all(promises).then(function() {
|
||||||
|
@ -575,6 +589,8 @@ window.textsecure.sendMessage = function() {
|
||||||
}
|
}
|
||||||
|
|
||||||
var registerError = function(number, message, error) {
|
var registerError = function(number, message, error) {
|
||||||
|
if (error.humanError)
|
||||||
|
message = error.humanError;
|
||||||
errors[errors.length] = { number: number, reason: message, error: error };
|
errors[errors.length] = { number: number, reason: message, error: error };
|
||||||
numberCompleted();
|
numberCompleted();
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,7 +100,11 @@ $('#init-go').click(function() {
|
||||||
registrationDone();
|
registrationDone();
|
||||||
}
|
}
|
||||||
}).catch(function(error) {
|
}).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__;
|
var StaticArrayBufferProto = new ArrayBuffer().__proto__;
|
||||||
function assertIsArrayBuffer(thing) {
|
function assertIsArrayBuffer(thing) {
|
||||||
if (thing !== Object(thing) || thing.__proto__ != StaticArrayBufferProto)
|
if (thing !== Object(thing) || thing.__proto__ != StaticArrayBufferProto)
|
||||||
throw new Error("WARNING: Needed a ArrayBuffer");
|
throw new Error("Needed a ArrayBuffer");
|
||||||
}
|
}
|
||||||
|
|
||||||
// private implementation functions
|
// private implementation functions
|
||||||
|
|
Loading…
Reference in a new issue