diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f9d0fdc --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +.swiftpm diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..dfca272 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Einstore + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..46909ac --- /dev/null +++ b/Package.swift @@ -0,0 +1,24 @@ +// swift-tools-version:5.1 +import PackageDescription + +let package = Package( + name: "WebError", + products: [ + .library(name: "WebErrorKit", targets: ["WebErrorKit"]) + ], + dependencies: [], + targets: [ + .target( + name: "WebErrorKit", + dependencies: [] + ), + .testTarget( + name: "WebErrorKitTests", + dependencies: [ + "WebErrorKit" + ] + ) + ] +) + + diff --git a/README.md b/README.md new file mode 100644 index 0000000..a9f667e --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# WebErrorKit + + + +### Install using SPM + +```swift +.package(url: "https://github.com/Einstore/WebErrorKit.git", from: "0.0.1") +``` + +> Discover more commands under `shell.cmd` and `shell.cmd.install` + +### Author + +Ondrej Rafaj @rafiki270 + +### License + +MIT; Copyright 2019 - Einstore diff --git a/Sources/WebErrorKit/Exports.swift b/Sources/WebErrorKit/Exports.swift new file mode 100644 index 0000000..ff2b4e2 --- /dev/null +++ b/Sources/WebErrorKit/Exports.swift @@ -0,0 +1 @@ +@_exported import Foundation diff --git a/Sources/WebErrorKit/Extensions/String+Codify.swift b/Sources/WebErrorKit/Extensions/String+Codify.swift new file mode 100644 index 0000000..154df52 --- /dev/null +++ b/Sources/WebErrorKit/Extensions/String+Codify.swift @@ -0,0 +1,14 @@ +extension String { + + public func snake_cased() -> String? { + let pattern = "([a-z0-9])([A-Z])" + + let regex = try? NSRegularExpression(pattern: pattern, options: []) + let range = NSRange(location: 0, length: count) + return regex? + .stringByReplacingMatches(in: self, options: [], range: range, withTemplate: "$1_$2") + .replacingOccurrences(of: " ", with: "_") + .lowercased() + } + +} diff --git a/Sources/WebErrorKit/Extensions/WebError+RawRepresentable.swift b/Sources/WebErrorKit/Extensions/WebError+RawRepresentable.swift new file mode 100644 index 0000000..f6843cc --- /dev/null +++ b/Sources/WebErrorKit/Extensions/WebError+RawRepresentable.swift @@ -0,0 +1,10 @@ +extension WebError where Self: RawRepresentable, RawValue == String { + + /// Default code + public var code: String { + let enumName = String(describing: type(of: self)) + let value = rawValue + return "\(enumName.snake_cased() ?? enumName).\(value.snake_cased() ?? value)" + } + +} diff --git a/Sources/WebErrorKit/Extensions/WebError+Serializable.swift b/Sources/WebErrorKit/Extensions/WebError+Serializable.swift new file mode 100644 index 0000000..ec21573 --- /dev/null +++ b/Sources/WebErrorKit/Extensions/WebError+Serializable.swift @@ -0,0 +1,9 @@ +extension SerializableWebError { + + /// Default code + public var code: String { + let c = serializedCode + return "\(snake_type).\(c.snake_cased() ?? c)" + } + +} diff --git a/Sources/WebErrorKit/SerializableWebError.swift b/Sources/WebErrorKit/SerializableWebError.swift new file mode 100644 index 0000000..5448efa --- /dev/null +++ b/Sources/WebErrorKit/SerializableWebError.swift @@ -0,0 +1,7 @@ +/// Serializable WebError for complex errors +public protocol SerializableWebError: WebError { + + /// Serialized error code + var serializedCode: String { get } + +} diff --git a/Sources/WebErrorKit/WebError.swift b/Sources/WebErrorKit/WebError.swift new file mode 100644 index 0000000..74a4ec6 --- /dev/null +++ b/Sources/WebErrorKit/WebError.swift @@ -0,0 +1,36 @@ +/// Web error +public protocol WebError: Error { + + /// Error status code (default 500) + var statusCode: Int { get } + + /// Error code + var code: String { get } + + /// Reason for failure (defalt nil) + var reason: String? { get } + +} + +extension WebError { + + /// Default status code + public var statusCode: Int { + return 500 + } + + /// Default reason + public var reason: String? { + return localizedDescription + } + + /// Snake cased Type + public var snake_type: String { + let name = String(describing: type(of: self)) + guard let snake = name.snake_cased() else { + return name + } + return snake + } + +} diff --git a/Sources/WebErrorKit/WebErrorContent.swift b/Sources/WebErrorKit/WebErrorContent.swift new file mode 100644 index 0000000..b6a577f --- /dev/null +++ b/Sources/WebErrorKit/WebErrorContent.swift @@ -0,0 +1,18 @@ +/// Codable error content object +public struct WebErrorContent: Codable { + + /// Error code + public let code: String + + /// Reason for failure + public let reason: String? + + /// Initializer + /// - Parameter code: Error code + /// - Parameter reason: Reason for failure + public init(code: String, reason: String? = nil) { + self.code = code + self.reason = reason + } + +} diff --git a/Tests/WebErrorKitTests/Tests.swift b/Tests/WebErrorKitTests/Tests.swift new file mode 100644 index 0000000..2c54c6f --- /dev/null +++ b/Tests/WebErrorKitTests/Tests.swift @@ -0,0 +1,47 @@ +import XCTest +import WebErrorKit + + +enum MyError: String, WebError { + case single + case camelCase + case snake_case +} + +enum MyComplexError: SerializableWebError { + + case itsComplicated(complication: String) + + var serializedCode: String { + switch self { + case .itsComplicated(complication: let c): + return "its_complicated:\(c)" + } + } + +} + + +class Tests: XCTestCase { + + func testSingle() { + let err = MyError.single.code + XCTAssertEqual(err, "my_error.single") + } + + func testCamelCase() { + let err = MyError.camelCase.code + XCTAssertEqual(err, "my_error.camel_case") + } + + func testSnakeCase() { + let err = MyError.snake_case.code + XCTAssertEqual(err, "my_error.snake_case") + } + + func testComplicatedCase() { + let err = MyComplexError.itsComplicated(complication: "Huge problem").code + XCTAssertEqual(err, "my_complex_error.its_complicated:huge_problem") + } + +}