-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 183170b
Showing
12 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.DS_Store | ||
.swiftpm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
] | ||
) | ||
] | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
@_exported import Foundation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
Sources/WebErrorKit/Extensions/WebError+RawRepresentable.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)" | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
extension SerializableWebError { | ||
|
||
/// Default code | ||
public var code: String { | ||
let c = serializedCode | ||
return "\(snake_type).\(c.snake_cased() ?? c)" | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/// Serializable WebError for complex errors | ||
public protocol SerializableWebError: WebError { | ||
|
||
/// Serialized error code | ||
var serializedCode: String { get } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} | ||
|
||
} |