-
Notifications
You must be signed in to change notification settings - Fork 109
Storage hardening; Add error handling capabilities. #163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
b2f62a1
9e85e20
81cc70b
436eb5d
5aa9781
cb6dc7f
250a157
e7c06ed
c4c8172
e6e43c7
7a9bb21
82faf28
36d981e
13cf429
8a89b3d
0d34e46
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // | ||
| // Errors.swift | ||
| // | ||
| // | ||
| // Created by Brandon Sneed on 10/20/22. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| public enum AnalyticsError: Error { | ||
| case storageUnableToCreate(String) | ||
| case storageUnableToWrite(String) | ||
| case storageUnableToRename(String) | ||
| case storageUnableToOpen(String) | ||
| case storageInvalid(String) | ||
| case storageUnknown(Error) | ||
|
|
||
| case networkUnexpectedHTTPCode(Int) | ||
| case networkServerLimited(Int) | ||
| case networkServerRejected(Int) | ||
| case networkUnknown(Error) | ||
| case networkInvalidData | ||
|
|
||
| case jsonUnableToSerialize(Error) | ||
| case jsonUnableToDeserialize(Error) | ||
| case jsonUnknown(Error) | ||
|
|
||
| case pluginError(Error) | ||
| } | ||
|
|
||
| extension Analytics { | ||
| /// Tries to convert known error types to AnalyticsError. | ||
| static internal func translate(error: Error) -> Error { | ||
| if let e = error as? OutputFileStream.OutputStreamError { | ||
| switch e { | ||
| case .invalidPath(let path): | ||
| return AnalyticsError.storageInvalid(path) | ||
| case .unableToCreate(let path): | ||
| return AnalyticsError.storageUnableToCreate(path) | ||
| case .unableToOpen(let path): | ||
| return AnalyticsError.storageUnableToOpen(path) | ||
| case .unableToWrite(let path): | ||
| return AnalyticsError.storageUnableToWrite(path) | ||
| } | ||
| } | ||
|
|
||
| if let e = error as? JSON.JSONError { | ||
| switch e { | ||
| case .incorrectType: | ||
| return AnalyticsError.jsonUnableToDeserialize(e) | ||
| case .nonJSONType: | ||
| return AnalyticsError.jsonUnableToDeserialize(e) | ||
| case .unknown: | ||
| return AnalyticsError.jsonUnknown(e) | ||
| } | ||
| } | ||
| return error | ||
| } | ||
|
|
||
| /// Reports an internal error to the user-defined error handler. | ||
| public func reportInternalError(_ error: Error, hardStop: Bool = false) { | ||
|
||
| let translatedError = Self.translate(error: error) | ||
| configuration.values.errorHandler?(translatedError) | ||
| Self.segmentLog(message: "An internal error occurred: \(translatedError)", kind: .error) | ||
| if hardStop { | ||
| exceptionFailure("A critical error occurred: \(translatedError)") | ||
| } | ||
| } | ||
|
|
||
| static public func reportInternalError(_ error: Error, hardStop: Bool = false) { | ||
| // we don't have an instance of analytics to call to get our error handler, | ||
| // but we can at least report the message to the console. | ||
| let translatedError = Self.translate(error: error) | ||
| Self.segmentLog(message: "An internal error occurred: \(translatedError)", kind: .error) | ||
| if hardStop { | ||
| exceptionFailure("A critical error occurred: \(translatedError)") | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious as to why it returns the
Configuration?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The config methods are all chainable and this continues that. Makes is so you can do ...
.. instead of having to create an instance and then set them all afterwards.