syncfiles/macos/error.h
Dietrich Epp b1bcae531b Rework error handling
Previously, various error handling functions were defined, but not all
of them were even used. The new error handling functions handle the most
common cases:

- Assertions. These can be handled with `ASSERT()`, which gives the
  error location for debugging.

- Errors we don't know how to handle, like GetNewWindow returning NULL.
  These can be handled with `EXIT_INTERNAL()`, which gives the error
  location for debugging.

- Out of memory conditions. These can be handled with `ShowMemError()`.

- System errors in response to user operations. These can be handled
  with `ShowError()`.
2023-05-06 19:41:40 -04:00

60 lines
2.1 KiB
C

// 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.
#ifndef MACOS_ERROR_H
#define MACOS_ERROR_H
// Error codes, corresponding to messages in a STR# resource. This should be
// kept in sync with STR# rSTRS_Errors in resources.r.
typedef enum ErrorCode {
// (no error)
kErrNone,
// Out of memory.
kErrOutOfMemory,
// Could not save project "^1".
kErrCouldNotSaveProject,
// Could not read project "^1".
kErrCouldNotReadProject,
// The project file is damaged.
kErrProjectDamaged,
// The project file is from an unknown version of SyncFiles.
kErrProjectUnknownVersion,
// Could not query volume parameters.
kErrVolumeQuery,
// Could not create alias.
kErrAlias,
// Could not get directory path.
kErrDirPath
} ErrorCode;
// ExitAssert shows an assertion error and quits the program. Either the file or
// the assertion may be NULL.
void ExitAssert(const unsigned char *file, int line,
const unsigned char *assertion);
// EXIT_INTERNAL shows an internal error message and quits the program.
#define EXIT_INTERNAL() ExitAssert("\p" __FILE__, __LINE__, NULL)
// EXIT_ASSERT shows an assertion error and quits the program. The message may
// be NULL.
#define EXIT_ASSERT(str) ExitAssert("\p" __FILE__, __LINE__, str)
// ASSERT checks that the given condition is true. Otherwise, it displays an
// error message and quits the program.
#define ASSERT(p) \
do { \
if (!(p)) \
ExitAssert("\p" __FILE__, __LINE__, "\p" #p); \
} while (0)
// ShowMemError shows an out of memory error to the user.
void ShowMemError(void);
// ShowError shows an error alert window to the user. The error codes are
// displayed if they are not 0. If osErr is not 0, then it is displayed as well.
// Any ^1 parameters in the error messages are replaced with strParam.
void ShowError(ErrorCode err1, ErrorCode err2, short osErr,
const unsigned char *strParam);
#endif