Improve file errors so that it doesn't look like everything has failed when we see there's no https cert available.

This commit is contained in:
Cory McWilliams 2024-04-28 12:25:12 -04:00
parent be85a620ef
commit 4cbda7a849
2 changed files with 24 additions and 13 deletions

View File

@ -1491,8 +1491,15 @@ loadSettings()
async function start_tls() { async function start_tls() {
const kCertificatePath = 'data/httpd/certificate.pem'; const kCertificatePath = 'data/httpd/certificate.pem';
const kPrivateKeyPath = 'data/httpd/privatekey.pem'; const kPrivateKeyPath = 'data/httpd/privatekey.pem';
let privateKey = utf8Decode(await File.readFile(kPrivateKeyPath)); let privateKey;
let certificate = utf8Decode(await File.readFile(kCertificatePath)); let certificate;
try {
privateKey = utf8Decode(await File.readFile(kPrivateKeyPath));
certificate = utf8Decode(await File.readFile(kCertificatePath));
} catch (e) {
print(`TLS disabled (${e.message}).`);
return;
}
let context = new TlsContext(); let context = new TlsContext();
context.setPrivateKey(privateKey); context.setPrivateKey(privateKey);
context.setCertificate(certificate); context.setCertificate(certificate);

View File

@ -78,7 +78,7 @@ static void _file_write_write_callback(uv_fs_t* req)
} }
else else
{ {
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, "%s", uv_strerror(req->result))); tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, "Failed to write %s: %s", req->path, uv_strerror(req->result)));
} }
int result = uv_fs_close(req->loop, req, fsreq->file, _file_async_close_callback); int result = uv_fs_close(req->loop, req, fsreq->file, _file_async_close_callback);
if (result < 0) if (result < 0)
@ -91,6 +91,7 @@ static void _file_write_write_callback(uv_fs_t* req)
static void _file_write_open_callback(uv_fs_t* req) static void _file_write_open_callback(uv_fs_t* req)
{ {
fs_req_t* fsreq = (fs_req_t*)req; fs_req_t* fsreq = (fs_req_t*)req;
const char* path = tf_strdup(req->path);
uv_fs_req_cleanup(req); uv_fs_req_cleanup(req);
tf_task_t* task = req->loop->data; tf_task_t* task = req->loop->data;
JSContext* context = tf_task_get_context(task); JSContext* context = tf_task_get_context(task);
@ -102,7 +103,8 @@ static void _file_write_open_callback(uv_fs_t* req)
int result = uv_fs_write(req->loop, req, fsreq->file, &buf, 1, 0, _file_write_write_callback); int result = uv_fs_write(req->loop, req, fsreq->file, &buf, 1, 0, _file_write_write_callback);
if (result < 0) if (result < 0)
{ {
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, "%s", uv_strerror(result))); uv_fs_req_cleanup(req);
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, "Failed to write %s: %s", path, uv_strerror(result)));
result = uv_fs_close(req->loop, req, fsreq->file, _file_async_close_callback); result = uv_fs_close(req->loop, req, fsreq->file, _file_async_close_callback);
if (result < 0) if (result < 0)
{ {
@ -114,9 +116,9 @@ static void _file_write_open_callback(uv_fs_t* req)
else else
{ {
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, "%s", uv_strerror(req->result))); tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, "%s", uv_strerror(req->result)));
uv_fs_req_cleanup(req);
tf_free(req); tf_free(req);
} }
tf_free((void*)path);
} }
static JSValue _file_write_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) static JSValue _file_write_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
@ -156,7 +158,7 @@ static JSValue _file_write_file(JSContext* context, JSValueConst this_val, int a
int result = uv_fs_open(tf_task_get_loop(task), &req->fs, file_name, UV_FS_O_CREAT | UV_FS_O_WRONLY, 0644, _file_write_open_callback); int result = uv_fs_open(tf_task_get_loop(task), &req->fs, file_name, UV_FS_O_CREAT | UV_FS_O_WRONLY, 0644, _file_write_open_callback);
if (result < 0) if (result < 0)
{ {
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, "%s", uv_strerror(result))); tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, "Failed to open %s for write: %s", file_name, uv_strerror(result)));
} }
JS_FreeCString(context, file_name); JS_FreeCString(context, file_name);
return promise_value; return promise_value;
@ -164,7 +166,6 @@ static JSValue _file_write_file(JSContext* context, JSValueConst this_val, int a
static void _file_read_read_callback(uv_fs_t* req) static void _file_read_read_callback(uv_fs_t* req)
{ {
uv_fs_req_cleanup(req);
fs_req_t* fsreq = (fs_req_t*)req; fs_req_t* fsreq = (fs_req_t*)req;
tf_task_t* task = req->loop->data; tf_task_t* task = req->loop->data;
JSContext* context = tf_task_get_context(task); JSContext* context = tf_task_get_context(task);
@ -177,8 +178,9 @@ static void _file_read_read_callback(uv_fs_t* req)
} }
else else
{ {
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, "%s", uv_strerror(req->result))); tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, "Failed to read %s: %s", req->path, uv_strerror(req->result)));
} }
uv_fs_req_cleanup(req);
int result = uv_fs_close(req->loop, req, fsreq->file, _file_async_close_callback); int result = uv_fs_close(req->loop, req, fsreq->file, _file_async_close_callback);
if (result < 0) if (result < 0)
{ {
@ -189,6 +191,7 @@ static void _file_read_read_callback(uv_fs_t* req)
static void _file_read_open_callback(uv_fs_t* req) static void _file_read_open_callback(uv_fs_t* req)
{ {
const char* path = tf_strdup(req->path);
uv_fs_req_cleanup(req); uv_fs_req_cleanup(req);
fs_req_t* fsreq = (fs_req_t*)req; fs_req_t* fsreq = (fs_req_t*)req;
tf_task_t* task = req->loop->data; tf_task_t* task = req->loop->data;
@ -201,7 +204,7 @@ static void _file_read_open_callback(uv_fs_t* req)
int result = uv_fs_read(req->loop, req, fsreq->file, &buf, 1, 0, _file_read_read_callback); int result = uv_fs_read(req->loop, req, fsreq->file, &buf, 1, 0, _file_read_read_callback);
if (result < 0) if (result < 0)
{ {
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, "%s", uv_strerror(result))); tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, "Failed to read %s: %s", path, uv_strerror(result)));
result = uv_fs_close(req->loop, req, fsreq->file, _file_async_close_callback); result = uv_fs_close(req->loop, req, fsreq->file, _file_async_close_callback);
if (result < 0) if (result < 0)
{ {
@ -212,10 +215,11 @@ static void _file_read_open_callback(uv_fs_t* req)
} }
else else
{ {
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, "%s", uv_strerror(req->result))); tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, "Failed to open %s for read: %s", path, uv_strerror(req->result)));
uv_fs_req_cleanup(req); uv_fs_req_cleanup(req);
tf_free(req); tf_free(req);
} }
tf_free((void*)path);
} }
static JSValue _file_read_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) static JSValue _file_read_file(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
@ -238,7 +242,7 @@ static JSValue _file_read_file(JSContext* context, JSValueConst this_val, int ar
int result = uv_fs_open(tf_task_get_loop(task), &req->fs, file_name, UV_FS_O_RDONLY, 0, _file_read_open_callback); int result = uv_fs_open(tf_task_get_loop(task), &req->fs, file_name, UV_FS_O_RDONLY, 0, _file_read_open_callback);
if (result < 0) if (result < 0)
{ {
tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, "%s", uv_strerror(result))); tf_task_reject_promise(task, promise, JS_ThrowInternalError(context, "Failed to open %s for read: %s", file_name, uv_strerror(result)));
uv_fs_req_cleanup(&req->fs); uv_fs_req_cleanup(&req->fs);
tf_free(req); tf_free(req);
} }
@ -322,7 +326,7 @@ static void _file_read_file_zip_after_work(uv_work_t* work, int status)
} }
else else
{ {
tf_task_reject_promise(data->task, data->promise, JS_ThrowInternalError(data->context, "Error: %d.", data->result)); tf_task_reject_promise(data->task, data->promise, JS_ThrowInternalError(data->context, "Failed to read %s: %d.", data->file_path, data->result));
} }
tf_free(data->buffer); tf_free(data->buffer);
tf_free((void*)data->file_path); tf_free((void*)data->file_path);
@ -352,7 +356,7 @@ static JSValue _file_read_file_zip(JSContext* context, JSValueConst this_val, in
int r = uv_queue_work(tf_task_get_loop(task), &work->request, _file_read_file_zip_work, _file_read_file_zip_after_work); int r = uv_queue_work(tf_task_get_loop(task), &work->request, _file_read_file_zip_work, _file_read_file_zip_after_work);
if (r) if (r)
{ {
tf_task_reject_promise(task, work->promise, JS_ThrowInternalError(context, "%s", uv_strerror(r))); tf_task_reject_promise(task, work->promise, JS_ThrowInternalError(context, "Failed to create read work for %s: %s", file_name, uv_strerror(r)));
tf_free((void*)work->file_path); tf_free((void*)work->file_path);
tf_free(work); tf_free(work);
} }