Skip to content

Commit 7eb81b5

Browse files
committed
Add ApiException::fromException builder to avoid conversion boilerplate
1 parent 8d1bfbc commit 7eb81b5

3 files changed

Lines changed: 25 additions & 20 deletions

File tree

api/exceptions.inc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ class ApiException extends Exception
1414
) {
1515
parent::__construct($message, $code);
1616
}
17+
18+
public static function fromException(Exception $e): self
19+
{
20+
return new self($e->getMessage(), $e->getCode());
21+
}
1722
}
1823

1924
//---------------------------------------------------------------------------

api/v1_projects.inc

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ function create_or_update_project(Project $project)
264264

265265
$project->validate(true);
266266
} catch (ProjectException $exception) {
267-
throw new InvalidValue($exception->getMessage(), $exception->getCode());
267+
throw InvalidValue::fromException($exception);
268268
}
269269

270270
// validate charsuites
@@ -513,7 +513,7 @@ function api_v1_project_pages(string $method, array $data)
513513
];
514514
}
515515
} catch (NoProjectPageTable $exception) {
516-
throw new NotFoundError($exception->getMessage(), $exception->getCode());
516+
throw NotFoundError::fromException($exception);
517517
}
518518
return $return_data;
519519
}
@@ -545,7 +545,7 @@ function api_v1_project_pagedetails(string $method, array $data, array $query_pa
545545
try {
546546
$rounds_to_display = get_rounds_to_display($project);
547547
} catch (NoProjectPageTable $exception) {
548-
throw new NotFoundError($exception->getMessage(), $exception->getCode());
548+
throw NotFoundError::fromException($exception);
549549
}
550550
if (!is_null($only_rounds)) {
551551
$rounds_to_display = array_intersect($rounds_to_display, $only_rounds);
@@ -849,9 +849,9 @@ function api_v1_project_checkout(string $method, array $data, array $query_param
849849
$proof_project = new ProofProject($project);
850850
return $proof_project->checkout();
851851
} catch (ProjectException $exception) {
852-
throw new NotFoundError($exception->getMessage(), $exception->getCode());
852+
throw NotFoundError::fromException($exception);
853853
} catch (UserAccessException $exception) {
854-
throw new ForbiddenError($exception->getMessage(), $exception->getCode());
854+
throw ForbiddenError::fromException($exception);
855855
}
856856
}
857857

@@ -933,9 +933,9 @@ function api_v1_project_page(string $method, array $data, array $query_params)
933933
throw new BadRequest("$page_action is not a valid page action.");
934934
}
935935
} catch (ProjectException $exception) {
936-
throw new NotFoundError($exception->getMessage(), $exception->getCode());
936+
throw NotFoundError::fromException($exception);
937937
} catch (UserAccessException $exception) {
938-
throw new ForbiddenError($exception->getMessage(), $exception->getCode());
938+
throw ForbiddenError::fromException($exception);
939939
}
940940
}
941941

@@ -950,9 +950,9 @@ function api_v1_project_page_format_preview(string $method, array $data): void
950950

951951
$proof_project_page->fp_report();
952952
} catch (ProjectException $exception) {
953-
throw new NotFoundError($exception->getMessage(), $exception->getCode());
953+
throw NotFoundError::fromException($exception);
954954
} catch (UserAccessException $exception) {
955-
throw new ForbiddenError($exception->getMessage(), $exception->getCode());
955+
throw ForbiddenError::fromException($exception);
956956
}
957957
}
958958

@@ -976,11 +976,11 @@ function api_v1_project_page_report_bad(string $method, array $data): void
976976
}
977977
$proof_project_page->markAsBad($pguser, $reason_key);
978978
} catch (ProjectTransitionException $exception) {
979-
throw new UnexpectedError($exception->getMessage(), $exception->getCode());
979+
throw UnexpectedError::fromException($exception);
980980
} catch (ProjectException $exception) {
981-
throw new BadRequest($exception->getMessage(), $exception->getCode());
981+
throw BadRequest::fromException($exception);
982982
} catch (UserAccessException $exception) {
983-
throw new ForbiddenError($exception->getMessage(), $exception->getCode());
983+
throw ForbiddenError::fromException($exception);
984984
}
985985
}
986986

@@ -995,9 +995,9 @@ function api_v1_project_page_wordcheck(string $method, array $data): void
995995

996996
$proof_project_page->wc_report(receive_data_from_request_body("accepted_words") ?? []);
997997
} catch (ProjectException $exception) {
998-
throw new NotFoundError($exception->getMessage(), $exception->getCode());
998+
throw NotFoundError::fromException($exception);
999999
} catch (UserAccessException $exception) {
1000-
throw new ForbiddenError($exception->getMessage(), $exception->getCode());
1000+
throw ForbiddenError::fromException($exception);
10011001
}
10021002
}
10031003

api/v1_validators.inc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function validate_stage(string $stageid, array $_data): Stage
1414
}
1515
return $stage;
1616
} catch (InvalidValue $exception) {
17-
throw new NotFoundError($exception->getMessage(), $exception->getCode());
17+
throw NotFoundError::fromException($exception);
1818
}
1919
}
2020

@@ -27,7 +27,7 @@ function validate_round(string $roundid, array $_data): Round
2727
}
2828
return $round;
2929
} catch (InvalidValue $exception) {
30-
throw new NotFoundError($exception->getMessage(), $exception->getCode());
30+
throw NotFoundError::fromException($exception);
3131
}
3232
}
3333

@@ -37,7 +37,7 @@ function validate_project(string $projectid, array $_data): Project
3737
try {
3838
return new Project($projectid);
3939
} catch (NonexistentProjectException $exception) {
40-
throw new NotFoundError($exception->getMessage(), $exception->getCode());
40+
throw NotFoundError::fromException($exception);
4141
}
4242
}
4343

@@ -54,7 +54,7 @@ function validate_page_name(string $pagename, array $data): ProjectPage
5454
try {
5555
return $data[":projectid"]->get_project_page($pagename);
5656
} catch (NonexistentPageException | NoProjectPageTable $exception) {
57-
throw new NotFoundError($exception->getMessage(), $exception->getCode());
57+
throw NotFoundError::fromException($exception);
5858
}
5959
}
6060

@@ -68,7 +68,7 @@ function validate_page_round(string $pageround, array $_data): string
6868
}
6969
return $pageround;
7070
} catch (InvalidPageRoundException $exception) {
71-
throw new NotFoundError($exception->getMessage(), $exception->getCode());
71+
throw NotFoundError::fromException($exception);
7272
}
7373
}
7474

@@ -104,6 +104,6 @@ function validate_username(string $username): User
104104
try {
105105
return new User($username);
106106
} catch (NonexistentUserException | NonuniqueUserException $exception) {
107-
throw new NotFoundError($exception->getMessage(), $exception->getCode());
107+
throw NotFoundError::fromException($exception);
108108
}
109109
}

0 commit comments

Comments
 (0)