Skip to content

Commit

Permalink
Feat: Add the error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
czs108 committed Feb 6, 2024
1 parent 852dcb0 commit 1cfe4ad
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
64 changes: 64 additions & 0 deletions include/utility/error_handling.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @file error_handling.h
* @brief Error handling.
* @author Chen Zhenshuo (chenzs108@outlook.com)
* @version 1.0
* @date 2020-01-09
*
* @par GitHub
* https://github.com/czs108/
*
* @see https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes/
*/

#pragma once

#include <windows.h>

/**
* @brief Get the last error code.
*
* @return The error code.
*/
DWORD GetLastErrorCode();


/**
* @brief Set the last error code got from `GetLastError` API.
*
* @param error_code The error code.
*
* @warning This method can only and must be called after a direct Windows API call failed.
*
* @attention
* Do not recommend using this method when freeing resources like memory and handles.
* The failure of related APIs may cover the error during initialization.
*/
void SetLastErrorCode();


/**
* @brief Reset the last error code to `ERROR_SUCCESS`.
*/
void ResetLastErrorCode();


/**
* @brief Get the message from the error code.
*
* @param error_code The error code got from `GetLastError` API.
* @return The message or `NULL` if the method failed.
*
* @warning The reture value must be free by `FreeErrorMessage()` method if it is not `NULL`.
*/
TCHAR *FormatErrorMessage(
const DWORD error_code);


/**
* @brief Free the memory of message.
*
* @param message The message got from `FormatErrorMessage()` method.
*/
void FreeErrorMessage(
TCHAR *const message);
2 changes: 2 additions & 0 deletions src/utility/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ target_include_directories(utility PUBLIC ${HEADER_PATH})

target_sources(utility
PRIVATE
${CMAKE_CURRENT_LIST_DIR}/error_handling.c
PUBLIC
${HEADER_PATH}/error_handling.h
)
58 changes: 58 additions & 0 deletions src/utility/error_handling.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @file error_handling.c
* @brief Error handling.
* @author Chen Zhenshuo (chenzs108@outlook.com)
* @version 1.0
* @date 2020-01-09
*
* @par GitHub
* https://github.com/czs108/
*
* @see https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes/
*/

#include "error_handling.h"

static DWORD last_error_code = ERROR_SUCCESS;


DWORD GetLastErrorCode()
{
return last_error_code;
}


void SetLastErrorCode()
{
last_error_code = GetLastError();
}


void ResetLastErrorCode()
{
last_error_code = ERROR_SUCCESS;
}


TCHAR *FormatErrorMessage(
const DWORD error_code)
{
TCHAR *const message = NULL;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, error_code, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
(LPTSTR)&message, 0, NULL);
return message;
}


void FreeErrorMessage(
TCHAR *const message)
{
if (message != NULL)
{
LocalFree(message);
}
}

0 comments on commit 1cfe4ad

Please sign in to comment.