Skip to content
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

REFACTOR: Throw single error type #108

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Sources/TMDb/Certifications/CertificationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ public final class CertificationService {
///
/// [TMDb API - Movie Certifications](https://developer.themoviedb.org/reference/certification-movie-list)
///
/// - Throws: TMDb data error ``TMDbError``.
///
/// - Returns: A dictionary of movie certifications.
///
public func movieCertifications() async throws -> [String: [Certification]] {
let certifications: Certifications
do {
certifications = try await apiClient.get(endpoint: CertificationsEndpoint.movie)
} catch let error {
throw error
throw TMDbError(error: error)
}

return certifications.certifications
Expand All @@ -44,14 +46,16 @@ public final class CertificationService {
///
/// [TMDb API - TV show Certifications](https://developer.themoviedb.org/reference/certifications-tv-list)
///
/// - Throws: TMDb data error ``TMDbError``.
///
/// - Returns: A dictionary of TV show certifications.
///
public func tvShowCertifications() async throws -> [String: [Certification]] {
let certifications: Certifications
do {
certifications = try await apiClient.get(endpoint: CertificationsEndpoint.tvShow)
} catch let error {
throw error
throw TMDbError(error: error)
}

return certifications.certifications
Expand Down
4 changes: 3 additions & 1 deletion Sources/TMDb/Company/CompanyService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ public final class CompanyService {
/// - Parameters:
/// - id: The identifier of the company.
///
/// - Throws: TMDb data error ``TMDbError``.
///
/// - Returns: Matching company.
///
public func details(forCompany id: Company.ID) async throws -> Company {
let company: Company
do {
company = try await apiClient.get(endpoint: CompanyEndpoint.details(companyID: id))
} catch let error {
throw error
throw TMDbError(error: error)
}

return company
Expand Down
18 changes: 13 additions & 5 deletions Sources/TMDb/Configuration/ConfigurationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ public final class ConfigurationService {
///
/// [TMDb API - Configuration: API Configuration](https://developers.themoviedb.org/3/configuration/get-api-configuration)
///
/// - Throws: TMDb data error ``TMDbError``.
///
/// - Returns: The API configuration.
///
public func apiConfiguration() async throws -> APIConfiguration {
let apiConfiguration: APIConfiguration
do {
apiConfiguration = try await apiClient.get(endpoint: ConfigurationEndpoint.api)
} catch let error {
throw error
throw TMDbError(error: error)
}

return apiConfiguration
Expand All @@ -45,14 +47,16 @@ public final class ConfigurationService {
///
/// [TMDb API - Configuration: Countries](https://developers.themoviedb.org/3/configuration/get-countries)
///
/// - Throws: TMDb data error ``TMDbError``.
///
/// - Returns: Countries used throughout TMDb,
///
public func countries() async throws -> [Country] {
let countries: [Country]
do {
countries = try await apiClient.get(endpoint: ConfigurationEndpoint.countries)
} catch let error {
throw error
throw TMDbError(error: error)
}

return countries
Expand All @@ -63,14 +67,16 @@ public final class ConfigurationService {
///
/// [TMDb API - Configuration: Jobs](https://developers.themoviedb.org/3/configuration/get-jobs)
///
/// - Throws: TMDb data error ``TMDbError``.
///
/// - Returns: Jobs and departments used on TMDb.
///
public func jobsByDepartment() async throws -> [Department] {
let departments: [Department]
do {
departments = try await apiClient.get(endpoint: ConfigurationEndpoint.jobs)
} catch let error {
throw error
throw TMDbError(error: error)
}

return departments
Expand All @@ -81,14 +87,16 @@ public final class ConfigurationService {
///
/// [TMDb API - Configuration: Languages](https://developers.themoviedb.org/3/configuration/get-languages)
///
/// - Returns: Languages used throughout TMDb.
/// - Throws: TMDb data error ``TMDbError``.
///
/// - Returns: Languages used throughout TMDb.
///
public func languages() async throws -> [Language] {
let languages: [Language]
do {
languages = try await apiClient.get(endpoint: ConfigurationEndpoint.languages)
} catch let error {
throw error
throw TMDbError(error: error)
}

return languages
Expand Down
8 changes: 6 additions & 2 deletions Sources/TMDb/Discover/DiscoverService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public final class DiscoverService {
/// - people: A list of Person identifiers which to return only movies they have appeared in.
/// - page: The page of results to return.
///
/// - Throws: TMDb data error ``TMDbError``.
///
/// - Returns: Matching movies as a pageable list.
///
public func movies(sortedBy: MovieSort? = nil, withPeople people: [Person.ID]? = nil,
Expand All @@ -43,7 +45,7 @@ public final class DiscoverService {
endpoint: DiscoverEndpoint.movies(sortedBy: sortedBy, people: people, page: page)
)
} catch let error {
throw error
throw TMDbError(error: error)
}

return movieList
Expand All @@ -60,14 +62,16 @@ public final class DiscoverService {
/// - sortedBy: How results should be sorted.
/// - page: The page of results to return.
///
/// - Throws: TMDb data error ``TMDbError``.
///
/// - Returns: Matching TV shows as a pageable list.
///
public func tvShows(sortedBy: TVShowSort? = nil, page: Int? = nil) async throws -> TVShowPageableList {
let tvShowList: TVShowPageableList
do {
tvShowList = try await apiClient.get(endpoint: DiscoverEndpoint.tvShows(sortedBy: sortedBy, page: page))
} catch let error {
throw error
throw TMDbError(error: error)
}

return tvShowList
Expand Down
8 changes: 6 additions & 2 deletions Sources/TMDb/Genres/GenreService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ public final class GenreService {
///
/// [TMDb API - Genres: Movies](https://developers.themoviedb.org/3/genres/get-movie-list)
///
/// - Throws: TMDb data error ``TMDbError``.
///
/// - Returns: A list of genres.
///
public func movieGenres() async throws -> [Genre] {
let genreList: GenreList
do {
genreList = try await apiClient.get(endpoint: GenresEndpoint.movie)
} catch let error {
throw error
throw TMDbError(error: error)
}

return genreList.genres
Expand All @@ -44,14 +46,16 @@ public final class GenreService {
///
/// [TMDb API - Genres: Movies](https://developers.themoviedb.org/3/genres/get-tv-list)
///
/// - Throws: TMDb data error ``TMDbError``.
///
/// - Returns: A list of genres.
///
public func tvShowGenres() async throws -> [Genre] {
let genreList: GenreList
do {
genreList = try await apiClient.get(endpoint: GenresEndpoint.tvShow)
} catch let error {
throw error
throw TMDbError(error: error)
}

return genreList.genres
Expand Down
16 changes: 0 additions & 16 deletions Sources/TMDb/Models/Movie.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,22 +229,6 @@ public struct Movie: Identifiable, Codable, Equatable, Hashable {

}

extension Movie: Comparable {

public static func < (lhs: Movie, rhs: Movie) -> Bool {
guard let lhsDate = lhs.releaseDate else {
return false
}

guard let rhsDate = rhs.releaseDate else {
return true
}

return lhsDate > rhsDate
}

}

extension Movie {

private enum CodingKeys: String, CodingKey {
Expand Down
23 changes: 23 additions & 0 deletions Sources/TMDb/Models/TMDbError+TMDbAPIError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Foundation

extension TMDbError {

init(error: Error) {
guard let apiError = error as? TMDbAPIError else {
self = .unknown
return
}

switch apiError {
case .notFound:
self = .notFound

case .network(let error):
self = .network(error)

default:
self = .unknown
}
}

}
Loading