Skip to content

Commit

Permalink
Merge pull request #26 from czuria1/feature/error-handling
Browse files Browse the repository at this point in the history
Add error handling for API errors
  • Loading branch information
Jakub-Vacek authored Dec 23, 2020
2 parents d731ea0 + c9e62ef commit 9720709
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 13 deletions.
6 changes: 6 additions & 0 deletions Pipeliner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
objects = {

/* Begin PBXBuildFile section */
725D03A5254F890B00ED0FEB /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = 725D03A4254F890B00ED0FEB /* Result.swift */; };
725D03A8254F895C00ED0FEB /* Result.swift in Sources */ = {isa = PBXBuildFile; fileRef = 725D03A4254F890B00ED0FEB /* Result.swift */; };
72B62F012532C763009DBE8A /* GitHubService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 72D86CA4252AB3880071A45B /* GitHubService.swift */; };
72B62F052532C9C9009DBE8A /* Workflow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 72B62F042532C9C9009DBE8A /* Workflow.swift */; };
72B62F082532CA5D009DBE8A /* Workflow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 72B62F042532C9C9009DBE8A /* Workflow.swift */; };
Expand Down Expand Up @@ -84,6 +86,7 @@
/* End PBXCopyFilesBuildPhase section */

/* Begin PBXFileReference section */
725D03A4254F890B00ED0FEB /* Result.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Result.swift; sourceTree = "<group>"; };
72B62F042532C9C9009DBE8A /* Workflow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Workflow.swift; sourceTree = "<group>"; };
72D86CA4252AB3880071A45B /* GitHubService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GitHubService.swift; sourceTree = "<group>"; };
F220499225011CED00ECC510 /* DateService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateService.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -251,6 +254,7 @@
F220499225011CED00ECC510 /* DateService.swift */,
F2E52EAA2512306E002A4692 /* Config.swift */,
F29E5B5C2513937500963075 /* ServiceInterface.swift */,
725D03A4254F890B00ED0FEB /* Result.swift */,
);
path = Common;
sourceTree = "<group>";
Expand Down Expand Up @@ -399,6 +403,7 @@
F26ECFB12506763100E62EDD /* PipelineDetailView.swift in Sources */,
F29E5B5D2513937500963075 /* ServiceInterface.swift in Sources */,
F2FF09952500BDA20058FBE7 /* ContentView.swift in Sources */,
725D03A5254F890B00ED0FEB /* Result.swift in Sources */,
F2FF09A62500BE600058FBE7 /* HttpService.swift in Sources */,
F2FF09B42500BF450058FBE7 /* ApiError.swift in Sources */,
F2FF09932500BDA20058FBE7 /* PipelinerApp.swift in Sources */,
Expand All @@ -423,6 +428,7 @@
files = (
72B62F082532CA5D009DBE8A /* Workflow.swift in Sources */,
F29E5B6125147D0B00963075 /* ServiceInterface.swift in Sources */,
725D03A8254F895C00ED0FEB /* Result.swift in Sources */,
F22049A02501409300ECC510 /* ServiceTypeEnum.swift in Sources */,
F220499E2501408800ECC510 /* ApiError.swift in Sources */,
F220499D2501407C00ECC510 /* HttpService.swift in Sources */,
Expand Down
11 changes: 6 additions & 5 deletions Pipeliner/Common/ApiError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
//

import Foundation
enum ApiError: Error {
case invalidUrl
case emptyResponse
case invalidDate
case configurationNotFound
enum ApiError: String, Error {
case invalidUrl = "Invalid URL"
case emptyResponse = "Empty response"
case invalidDate = "Invalid date"
case configurationNotFound = "Configuration not found"
case decodeError = "Decode error"
}
13 changes: 13 additions & 0 deletions Pipeliner/Common/Result.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// Result.swift
// Pipeliner
//
// Created by dx hero on 11/1/20.
//

import Foundation

enum Result<T> {
case success(T)
case failure(Error)
}
1 change: 1 addition & 0 deletions Pipeliner/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ struct ContentView: View {
} else {
Text("There are no data").font(.system(size: 18)).foregroundColor(Color("white-60"))
}

}.padding(.bottom)
}).padding(.vertical, 40).padding(.horizontal, 30)
}).padding(.bottom)
Expand Down
11 changes: 9 additions & 2 deletions Pipeliner/GitHub/GitHubService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ class GitHubService: IService {
var request = URLRequest(url: url)
request.setValue("application/vnd.github.v3+json", forHTTPHeaderField: "Accept")
request.setValue("bearer \(token)", forHTTPHeaderField:"Authorization")
let data = try await(httpService.getData(request: request))
let project = try JSONDecoder().decode(Project.self, from: data)

guard let data = try? await(httpService.getData(request: request)) else {
throw ApiError.emptyResponse
}

guard let project = try? JSONDecoder().decode(Project.self, from: data) else {
throw ApiError.decodeError
}

return project.name
}

Expand Down
24 changes: 18 additions & 6 deletions Pipeliner/GitLab/GitLabService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,15 @@ class GitLabService: IService {
}
var request = URLRequest(url: url)
request.setValue(token, forHTTPHeaderField:"PRIVATE-TOKEN")
let data = try await(httpService.getData(request: request))
let decoder = JSONDecoder()
let project = try decoder.decode(Project.self, from: data)

guard let data = try? await(httpService.getData(request: request)) else {
throw ApiError.emptyResponse
}

guard let project = try? JSONDecoder().decode(Project.self, from: data) else {
throw ApiError.decodeError
}

return project.name
}

Expand All @@ -35,7 +41,13 @@ class GitLabService: IService {
}
var request = URLRequest(url: url)
request.setValue(config.token, forHTTPHeaderField:"PRIVATE-TOKEN")
let data = try await(httpService.getData(request: request))
let decoder = JSONDecoder()
return try decoder.decode([Pipeline].self, from: data)
guard let data = try? await(httpService.getData(request: request)) else {
throw ApiError.emptyResponse
}

guard let pipelines = try? JSONDecoder().decode([Pipeline].self, from: data) else {
throw ApiError.decodeError
}

return pipelines
}}

0 comments on commit 9720709

Please # to comment.