|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
| 15 | +import Dependencies |
15 | 16 | import Fluent |
16 | 17 | import PostgresKit |
17 | 18 | import Vapor |
@@ -168,13 +169,17 @@ func ingest(client: Client, |
168 | 169 | extension Ingestion { |
169 | 170 | static func ingest(client: Client, database: Database, package: Joined<Package, Repository>) async { |
170 | 171 | let result = await Result { () async throws(Ingestion.Error) -> Joined<Package, Repository> in |
| 172 | + @Dependency(\.environment) var environment |
171 | 173 | Current.logger().info("Ingesting \(package.package.url)") |
172 | 174 |
|
173 | 175 | // Even though we have a `Joined<Package, Repository>` as a parameter, we must not rely |
174 | 176 | // on `repository` for owner/name as it will be nil when a package is first ingested. |
175 | 177 | // The only way to get `owner` and `repository` here is by parsing them from the URL. |
176 | 178 | let (owner, repository) = try await run { |
177 | | - try Github.parseOwnerName(url: package.model.url) |
| 179 | + if environment.shouldFail(failureMode: .invalidURL) { |
| 180 | + throw Github.Error.invalidURL(package.model.url) |
| 181 | + } |
| 182 | + return try Github.parseOwnerName(url: package.model.url) |
178 | 183 | } rethrowing: { _ in |
179 | 184 | Ingestion.Error.invalidURL(packageId: package.model.id!, url: package.model.url) |
180 | 185 | } |
@@ -223,7 +228,12 @@ extension Ingestion { |
223 | 228 |
|
224 | 229 | static func findOrCreateRepository(on database: Database, for package: Joined<Package, Repository>) async throws(Ingestion.Error) -> Repository { |
225 | 230 | try await run { |
226 | | - try await Repository.findOrCreate(on: database, for: package.model) |
| 231 | + @Dependency(\.environment) var environment |
| 232 | + if environment.shouldFail(failureMode: .findOrCreateRepositoryFailed) { |
| 233 | + throw Abort(.internalServerError) |
| 234 | + } |
| 235 | + |
| 236 | + return try await Repository.findOrCreate(on: database, for: package.model) |
227 | 237 | } rethrowing: { |
228 | 238 | Ingestion.Error( |
229 | 239 | packageId: package.model.id!, |
@@ -251,6 +261,11 @@ extension Ingestion { |
251 | 261 |
|
252 | 262 |
|
253 | 263 | static func fetchMetadata(client: Client, package: Package, owner: String, repository: String) async throws(Github.Error) -> (Github.Metadata, Github.License?, Github.Readme?) { |
| 264 | + @Dependency(\.environment) var environment |
| 265 | + if environment.shouldFail(failureMode: .fetchMetadataFailed) { |
| 266 | + throw Github.Error.requestFailed(.internalServerError) |
| 267 | + } |
| 268 | + |
254 | 269 | async let metadata = try await Current.fetchMetadata(client, owner, repository) |
255 | 270 | async let license = await Current.fetchLicense(client, owner, repository) |
256 | 271 | async let readme = await Current.fetchReadme(client, owner, repository) |
@@ -286,6 +301,20 @@ func updateRepository(on database: Database, |
286 | 301 | readmeInfo: Github.Readme?, |
287 | 302 | s3Readme: S3Readme?, |
288 | 303 | fork: Fork? = nil) async throws(Ingestion.Error.UnderlyingError) { |
| 304 | + @Dependency(\.environment) var environment |
| 305 | + if environment.shouldFail(failureMode: .noRepositoryMetadata) { |
| 306 | + throw .noRepositoryMetadata(owner: repository.owner, name: repository.name) |
| 307 | + } |
| 308 | + if environment.shouldFail(failureMode: .repositorySaveFailed) { |
| 309 | + throw .repositorySaveFailed(owner: repository.owner, |
| 310 | + name: repository.name, |
| 311 | + details: "TestError") |
| 312 | + } |
| 313 | + if environment.shouldFail(failureMode: .repositorySaveUniqueViolation) { |
| 314 | + throw .repositorySaveUniqueViolation(owner: repository.owner, |
| 315 | + name: repository.name, |
| 316 | + details: "TestError") |
| 317 | + } |
289 | 318 | guard let repoMetadata = metadata.repository else { |
290 | 319 | throw .noRepositoryMetadata(owner: repository.owner, name: repository.name) |
291 | 320 | } |
|
0 commit comments