2022-04-10 04:22:58 -04:00
|
|
|
// Copyright 2022 Dietrich Epp.
|
|
|
|
// This file is part of SyncFiles. SyncFiles is licensed under the terms of the
|
|
|
|
// Mozilla Public License, version 2.0. See LICENSE.txt for details.
|
2022-11-16 18:19:05 -05:00
|
|
|
#include "macos/error.h"
|
2022-04-10 04:09:17 -04:00
|
|
|
|
2023-05-06 19:41:40 -04:00
|
|
|
#include "lib/defs.h"
|
2022-11-16 18:19:05 -05:00
|
|
|
#include "macos/main.h"
|
2023-05-06 19:41:40 -04:00
|
|
|
#include "macos/pstrbuilder.h"
|
2022-11-16 18:19:05 -05:00
|
|
|
#include "macos/resources.h"
|
2022-04-10 04:09:17 -04:00
|
|
|
|
|
|
|
#include <Dialogs.h>
|
|
|
|
#include <TextUtils.h>
|
|
|
|
|
2023-05-06 19:41:40 -04:00
|
|
|
enum {
|
|
|
|
// An error of type ^2 occurred.
|
2023-05-06 21:56:00 -04:00
|
|
|
kStrErrorCode = 1,
|
2023-05-06 19:41:40 -04:00
|
|
|
// Error at: ^1:^2
|
|
|
|
kStrErrorAt,
|
|
|
|
// Assertion: ^1
|
|
|
|
kStrAssertion,
|
|
|
|
// An internal error occurred.
|
|
|
|
kStrInternal,
|
|
|
|
// An unknown error occurred.
|
|
|
|
kStrUnknown,
|
|
|
|
// (Base value for error messages.)
|
|
|
|
kStrMessageBase
|
|
|
|
};
|
|
|
|
|
|
|
|
static void AppendErrorArray(struct PStrBuilder *str, int sep, int strNum,
|
|
|
|
int paramCount, const unsigned char *const *params)
|
2022-04-10 04:09:17 -04:00
|
|
|
{
|
|
|
|
Str255 message;
|
|
|
|
|
2023-05-06 19:41:40 -04:00
|
|
|
if (str->data[0] > 0 && sep != 0) {
|
|
|
|
PStrAppendChar(str, sep);
|
|
|
|
}
|
|
|
|
GetIndString(message, rSTRS_Errors, strNum);
|
2022-04-10 04:09:17 -04:00
|
|
|
if (message[0] == 0) {
|
2023-05-06 19:41:40 -04:00
|
|
|
PStrAppendChar(str, '?');
|
|
|
|
} else {
|
|
|
|
PStrAppendSubstitute(str, message, paramCount, params);
|
2022-04-10 04:09:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-06 19:41:40 -04:00
|
|
|
static void AppendError0(struct PStrBuilder *str, int sep, int strNum)
|
2022-04-10 04:09:17 -04:00
|
|
|
{
|
2023-05-06 19:41:40 -04:00
|
|
|
AppendErrorArray(str, sep, strNum, 0, NULL);
|
|
|
|
}
|
2022-04-10 04:09:17 -04:00
|
|
|
|
2023-05-06 19:41:40 -04:00
|
|
|
static void AppendError1(struct PStrBuilder *str, int sep, int strNum,
|
|
|
|
const unsigned char *strParam)
|
|
|
|
{
|
|
|
|
AppendErrorArray(str, sep, strNum, 1, &strParam);
|
2022-04-10 04:09:17 -04:00
|
|
|
}
|
|
|
|
|
2023-05-06 19:41:40 -04:00
|
|
|
static void AppendError2(struct PStrBuilder *str, int sep, int strNum,
|
|
|
|
const unsigned char *strParam, int intParam)
|
2022-04-10 04:09:17 -04:00
|
|
|
{
|
2023-05-06 19:41:40 -04:00
|
|
|
unsigned char num[16];
|
|
|
|
const unsigned char *params[2];
|
|
|
|
|
|
|
|
params[0] = strParam;
|
|
|
|
NumToString(intParam, num);
|
|
|
|
params[1] = num;
|
|
|
|
AppendErrorArray(str, sep, strNum, 2, params);
|
|
|
|
}
|
|
|
|
|
2023-05-06 21:56:00 -04:00
|
|
|
static void ShowErrorAlert(const unsigned char *message, int id)
|
2023-05-06 19:41:40 -04:00
|
|
|
{
|
2023-05-06 21:56:00 -04:00
|
|
|
ParamText(message, NULL, NULL, NULL);
|
|
|
|
Alert(id, NULL);
|
2022-04-10 04:09:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ExitAssert(const unsigned char *file, int line,
|
2023-05-06 19:41:40 -04:00
|
|
|
const unsigned char *assertion)
|
2022-04-10 04:09:17 -04:00
|
|
|
{
|
2023-05-06 19:41:40 -04:00
|
|
|
struct PStrBuilder str;
|
2022-04-10 04:09:17 -04:00
|
|
|
|
2023-05-06 19:41:40 -04:00
|
|
|
PStrInit(&str);
|
|
|
|
AppendError0(&str, 0, kStrInternal);
|
|
|
|
if (file != NULL) {
|
|
|
|
AppendError2(&str, '\r', kStrErrorAt, file, line);
|
2022-04-10 04:09:17 -04:00
|
|
|
}
|
2023-05-06 19:41:40 -04:00
|
|
|
if (assertion != NULL) {
|
|
|
|
AppendError1(&str, '\r', kStrAssertion, assertion);
|
|
|
|
}
|
2023-05-06 21:56:00 -04:00
|
|
|
ShowErrorAlert(str.data, rAlrtFatal);
|
2022-04-10 04:09:17 -04:00
|
|
|
QuitApp();
|
|
|
|
}
|
|
|
|
|
2023-05-06 19:41:40 -04:00
|
|
|
void ShowError(ErrorCode err1, ErrorCode err2, short osErr,
|
|
|
|
const unsigned char *strParam)
|
2022-04-10 04:09:17 -04:00
|
|
|
{
|
2023-05-06 19:41:40 -04:00
|
|
|
struct PStrBuilder str;
|
2022-04-10 04:09:17 -04:00
|
|
|
|
2023-05-06 19:41:40 -04:00
|
|
|
PStrInit(&str);
|
|
|
|
if (err1 != kErrNone) {
|
|
|
|
AppendError1(&str, ' ', kStrMessageBase - 1 + err1, strParam);
|
2022-04-10 04:09:17 -04:00
|
|
|
}
|
2023-05-06 19:41:40 -04:00
|
|
|
if (err2 != kErrNone) {
|
|
|
|
AppendError1(&str, ' ', kStrMessageBase - 1 + err2, strParam);
|
2022-04-10 04:09:17 -04:00
|
|
|
}
|
2023-05-06 19:41:40 -04:00
|
|
|
if (osErr != 0) {
|
|
|
|
AppendError2(&str, ' ', kStrErrorCode, NULL, osErr);
|
|
|
|
}
|
|
|
|
if (str.data[0] == 0) {
|
|
|
|
AppendError0(&str, ' ', kStrUnknown);
|
|
|
|
}
|
2023-05-06 21:56:00 -04:00
|
|
|
ShowErrorAlert(str.data, rAlrtError);
|
2023-05-06 19:41:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ShowMemError(void)
|
|
|
|
{
|
|
|
|
ShowError(kErrOutOfMemory, kErrNone, MemError(), NULL);
|
2022-04-10 04:09:17 -04:00
|
|
|
}
|