|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Arduino SA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include "StorageError.h" |
| 8 | +#include <cstring> |
| 9 | + |
| 10 | +StorageError::StorageError() : code_(StorageErrorCode::NONE) { |
| 11 | + message_[0] = '\0'; |
| 12 | +} |
| 13 | + |
| 14 | +StorageErrorCode StorageError::getCode() const { |
| 15 | + return code_; |
| 16 | +} |
| 17 | + |
| 18 | +const char* StorageError::getMessage() const { |
| 19 | + if (message_[0] != '\0') { |
| 20 | + return message_; |
| 21 | + } |
| 22 | + |
| 23 | + // Return default message based on error code |
| 24 | + switch (code_) { |
| 25 | + case StorageErrorCode::NONE: |
| 26 | + return "No error"; |
| 27 | + case StorageErrorCode::FILE_NOT_FOUND: |
| 28 | + return "File not found"; |
| 29 | + case StorageErrorCode::FOLDER_NOT_FOUND: |
| 30 | + return "Folder not found"; |
| 31 | + case StorageErrorCode::ALREADY_EXISTS: |
| 32 | + return "Already exists"; |
| 33 | + case StorageErrorCode::INVALID_PATH: |
| 34 | + return "Invalid path"; |
| 35 | + case StorageErrorCode::PERMISSION_DENIED: |
| 36 | + return "Permission denied"; |
| 37 | + case StorageErrorCode::READ_ERROR: |
| 38 | + return "Read error"; |
| 39 | + case StorageErrorCode::WRITE_ERROR: |
| 40 | + return "Write error"; |
| 41 | + case StorageErrorCode::SEEK_ERROR: |
| 42 | + return "Seek error"; |
| 43 | + case StorageErrorCode::OPEN_ERROR: |
| 44 | + return "Open error"; |
| 45 | + case StorageErrorCode::CLOSE_ERROR: |
| 46 | + return "Close error"; |
| 47 | + case StorageErrorCode::STORAGE_FULL: |
| 48 | + return "Storage full"; |
| 49 | + case StorageErrorCode::STORAGE_NOT_MOUNTED: |
| 50 | + return "Storage not mounted"; |
| 51 | + case StorageErrorCode::STORAGE_CORRUPTED: |
| 52 | + return "Storage corrupted"; |
| 53 | + case StorageErrorCode::STORAGE_NOT_FORMATTED: |
| 54 | + return "Storage not formatted"; |
| 55 | + case StorageErrorCode::INVALID_OPERATION: |
| 56 | + return "Invalid operation"; |
| 57 | + case StorageErrorCode::INVALID_MODE: |
| 58 | + return "Invalid mode"; |
| 59 | + case StorageErrorCode::BUFFER_OVERFLOW: |
| 60 | + return "Buffer overflow"; |
| 61 | + case StorageErrorCode::OUT_OF_MEMORY: |
| 62 | + return "Out of memory"; |
| 63 | + case StorageErrorCode::TIMEOUT: |
| 64 | + return "Timeout"; |
| 65 | + case StorageErrorCode::HARDWARE_ERROR: |
| 66 | + return "Hardware error"; |
| 67 | + case StorageErrorCode::NOT_INITIALIZED: |
| 68 | + return "Not initialized"; |
| 69 | + case StorageErrorCode::UNKNOWN_ERROR: |
| 70 | + default: |
| 71 | + return "Unknown error"; |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +bool StorageError::hasError() const { |
| 76 | + return code_ != StorageErrorCode::NONE; |
| 77 | +} |
| 78 | + |
| 79 | +void StorageError::setError(StorageErrorCode code, const char* message) { |
| 80 | + code_ = code; |
| 81 | + if (message != nullptr) { |
| 82 | + strncpy(message_, message, sizeof(message_) - 1); |
| 83 | + message_[sizeof(message_) - 1] = '\0'; |
| 84 | + } else { |
| 85 | + message_[0] = '\0'; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +void StorageError::clear() { |
| 90 | + code_ = StorageErrorCode::NONE; |
| 91 | + message_[0] = '\0'; |
| 92 | +} |
| 93 | + |
| 94 | +StorageError::operator bool() const { |
| 95 | + return hasError(); |
| 96 | +} |
0 commit comments