Skip to content

Commit

Permalink
All exceptions throw in compilation helpers should be of type Exercis…
Browse files Browse the repository at this point in the history
…eCompilationException
  • Loading branch information
Neloop committed Mar 25, 2019
1 parent dcd281e commit f192a88
Show file tree
Hide file tree
Showing 30 changed files with 155 additions and 98 deletions.
13 changes: 10 additions & 3 deletions app/V1Module/presenters/ExercisesConfigPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace App\V1Module\Presenters;

use App\Exceptions\ApiException;
use App\Exceptions\ExerciseCompilationException;
use App\Exceptions\ExerciseConfigException;
use App\Exceptions\ForbiddenRequestException;
use App\Exceptions\InvalidArgumentException;
use App\Exceptions\NotFoundException;
use App\Exceptions\ParseException;
use App\Helpers\ExerciseConfig\Helper;
use App\Helpers\ExerciseConfig\Loader;
use App\Helpers\ExerciseConfig\Transformer;
Expand Down Expand Up @@ -290,9 +293,11 @@ public function checkSetConfiguration(string $id) {
* @POST
* @Param(type="post", name="config", description="A list of basic high level exercise configuration", validation="array")
* @param string $id Identifier of the exercise
* @throws ExerciseConfigException
* @throws ForbiddenRequestException
* @throws NotFoundException
* @throws ExerciseConfigException
* @throws ApiException
* @throws ParseException
*/
public function actionSetConfiguration(string $id) {
$exercise = $this->exercises->findOrThrow($id);
Expand Down Expand Up @@ -341,7 +346,6 @@ public function checkGetVariablesForExerciseConfig(string $id) {
* @param string $id Identifier of the exercise
* @Param(type="post", name="runtimeEnvironmentId", validation="string:1..", description="Environment identifier", required=false)
* @Param(type="post", name="pipelinesIds", validation="array", description="Identifiers of selected pipelines for one test")
* @throws ForbiddenRequestException
* @throws NotFoundException
* @throws ExerciseConfigException
*/
Expand Down Expand Up @@ -427,9 +431,12 @@ public function checkSetHardwareGroupLimits(string $id, string $runtimeEnvironme
* @param string $id Identifier of the exercise
* @param string $runtimeEnvironmentId
* @param string $hwGroupId
* @throws ApiException
* @throws ExerciseConfigException
* @throws ForbiddenRequestException
* @throws NotFoundException
* @throws ExerciseConfigException
* @throws ParseException
* @throws ExerciseCompilationException
*/
public function actionSetHardwareGroupLimits(string $id, string $runtimeEnvironmentId, string $hwGroupId) {
/** @var Exercise $exercise */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use App\Responses\ZipFilesResponse;
use App\Security\ACL\IExercisePermissions;
use App\Security\ACL\IReferenceExerciseSolutionPermissions;
use Exception;
use Tracy\ILogger;

/**
Expand Down Expand Up @@ -476,7 +477,7 @@ private function finishSubmission(
try {
$generatorResult = $this->jobConfigGenerator
->generateJobConfig($this->getCurrentUser(), $exercise, $runtimeEnvironment, $compilationParams);
} catch (ExerciseConfigException | JobConfigStorageException $e) {
} catch (Exception $e) {
$submission = new ReferenceSolutionSubmission($referenceSolution, null,
"", $this->getCurrentUser(), $isDebug);
$this->referenceSubmissions->persist($submission, false);
Expand Down Expand Up @@ -514,7 +515,7 @@ private function finishSubmission(
"expectedTasksCount" => $generatorResult->getJobConfig()->getTasksCount()
]
];
} catch (SubmissionFailedException $e) {
} catch (Exception $e) {
$this->logger->log("Reference evaluation exception: " . $e->getMessage(), ILogger::EXCEPTION);
$failure = SubmissionFailure::forReferenceSubmission(SubmissionFailure::TYPE_BROKER_REJECT, $e->getMessage(), $submission);
$this->submissionFailures->persist($failure, false);
Expand Down
2 changes: 1 addition & 1 deletion app/V1Module/presenters/SubmitPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ private function finishSubmission(AssignmentSolution $solution, bool $isDebug =
$solution->getAssignment(),
$solution->getSolution()->getRuntimeEnvironment(),
$compilationParams);
} catch (ExerciseConfigException | JobConfigStorageException $e) {
} catch (Exception $e) {
$submission = new AssignmentSolutionSubmission($solution, "", $this->getCurrentUser(), $isDebug);
$this->assignmentSubmissions->persist($submission, false);
$this->submissionFailed($submission, $e->getMessage(), SubmissionFailure::TYPE_CONFIG_ERROR,
Expand Down
20 changes: 20 additions & 0 deletions app/exceptions/ExerciseCompilationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Exceptions;

use Nette\Http\IResponse;

/**
* Exception used in exercise compilation to job configuration. Used on internal
* errors during compilation.
*/
class ExerciseCompilationException extends ApiException {

/**
* Create instance with further description.
* @param string $msg description
*/
public function __construct(string $msg = 'Please contact system administrator') {
parent::__construct("Exercise compilation error - $msg", IResponse::S500_INTERNAL_SERVER_ERROR);
}
}
9 changes: 3 additions & 6 deletions app/helpers/Emails/Notifications/GeneralStatsEmailsSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

namespace App\Helpers\Notifications;

use App\Exceptions\ExerciseConfigException;
use App\Exceptions\InvalidStateException;
use App\Helpers\Emails\EmailLatteFactory;
use App\Helpers\Emails\EmailLocalizationHelper;
use App\Helpers\Emails\EmailLinkHelper;
use App\Helpers\GeneralStatsHelper;
use App\Helpers\GeneralStats;
use App\Model\Entity\AssignmentSolutionSubmission;
use App\Helpers\EmailHelper;
use DateTime;
use DateInterval;
Expand Down Expand Up @@ -38,14 +35,14 @@ class GeneralStatsEmailsSender {
* Constructor.
* @param EmailHelper $emailHelper
* @param array $params
* @throws ExerciseConfigException
* @throws InvalidStateException
*/
public function __construct(EmailHelper $emailHelper, array $params) {
$this->emailHelper = $emailHelper;
$this->sender = Arrays::get($params, ["emails", "from"], "noreply@recodex.mff.cuni.cz");
$recipient = Arrays::get($params, ["emails", "to"]);
if (!$recipient) {
throw new ExerciseConfigException("Missing recipient (To) address in GeneralStatsEmailsSender configuration.");
throw new InvalidStateException("Missing recipient (To) address in GeneralStatsEmailsSender configuration.");
}
$this->recipient = is_array($recipient) ? $recipient : [$recipient];
$this->subject = Arrays::get($params, ["emails", "subject"], "General Status Overview");
Expand Down
12 changes: 6 additions & 6 deletions app/helpers/ExerciseConfig/Compilation/BoxesSorter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace App\Helpers\ExerciseConfig\Compilation;

use App\Exceptions\ExerciseConfigException;
use App\Exceptions\ExerciseCompilationException;
use App\Helpers\ExerciseConfig\Compilation\Tree\MergeTree;
use App\Helpers\ExerciseConfig\Compilation\Tree\Node;
use App\Helpers\ExerciseConfig\Compilation\Tree\PortNode;
Expand All @@ -22,7 +22,7 @@ class BoxesSorter {
* Topological sort of given tree, stack oriented.
* @param MergeTree $mergeTree
* @return PortNode[]
* @throws ExerciseConfigException
* @throws ExerciseCompilationException
*/
private function topologicalSort(MergeTree $mergeTree): array {
// Okey, let's make this crystal clear...
Expand Down Expand Up @@ -66,7 +66,7 @@ private function topologicalSort(MergeTree $mergeTree): array {
continue;
}

throw new ExerciseConfigException("Cycle in tree detected in node '{$node->getBox()->getName()}'.");
throw new ExerciseCompilationException("Cycle in tree detected in node '{$node->getBox()->getName()}'.");
}

// visit current node
Expand Down Expand Up @@ -94,7 +94,7 @@ private function topologicalSort(MergeTree $mergeTree): array {
* Sort tree and return newly created rooted tree.
* @param MergeTree $mergeTree
* @return RootedTree
* @throws ExerciseConfigException
* @throws ExerciseCompilationException
*/
private function sortTree(MergeTree $mergeTree): RootedTree {

Expand Down Expand Up @@ -125,7 +125,7 @@ private function sortTree(MergeTree $mergeTree): RootedTree {
foreach ($sorted[$i]->getParents() as $parent) {
$index = array_search($parent, $sorted, true);
if ($index === false) {
throw new ExerciseConfigException("Malformed internal compilation structure. PortNode not found.");
throw new ExerciseCompilationException("Malformed internal compilation structure. PortNode not found.");
}
$current->addDependency($nodes[$index]);
}
Expand All @@ -142,7 +142,7 @@ private function sortTree(MergeTree $mergeTree): RootedTree {
* For each test sort its boxes to order which makes execution sense.
* @param MergeTree[] $tests
* @return RootedTree[]
* @throws ExerciseConfigException
* @throws ExerciseCompilationException
*/
public function sort(array $tests): array {

Expand Down
12 changes: 8 additions & 4 deletions app/helpers/ExerciseConfig/Compilation/PipelinesMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Helpers\ExerciseConfig\Compilation;

use App\Exceptions\ExerciseCompilationException;
use App\Exceptions\ExerciseConfigException;
use App\Exceptions\NotFoundException;
use App\Helpers\ExerciseConfig\Compilation\Tree\PortNode;
Expand Down Expand Up @@ -254,6 +255,7 @@ private function buildPipelineTree(string $pipelineId, string $testId, Pipeline
* @param CompilationParams $params
* @return MergeTree new instance of merge tree
* @throws ExerciseConfigException
* @throws ExerciseCompilationException
*/
private function processPipeline(string $testId, PipelineVars $pipelineVars,
CompilationContext $context, CompilationParams $params): MergeTree {
Expand All @@ -265,7 +267,7 @@ private function processPipeline(string $testId, PipelineVars $pipelineVars,
$pipelineConfig = $this->pipelinesCache->getNewPipelineConfig($pipelineId);
$pipelineFiles = $pipelineEntity->getHashedSupplementaryFiles();
} catch (NotFoundException $e) {
throw new ExerciseConfigException("Pipeline '$pipelineId' not found in environment");
throw new ExerciseCompilationException("Pipeline '$pipelineId' not found in environment");
}

// build tree for given pipeline
Expand All @@ -287,6 +289,7 @@ private function processPipeline(string $testId, PipelineVars $pipelineVars,
* @param CompilationParams $params
* @return MergeTree
* @throws ExerciseConfigException
* @throws ExerciseCompilationException
*/
private function processTest(string $testId, Test $test, CompilationContext $context,
CompilationParams $params): MergeTree {
Expand All @@ -298,7 +301,7 @@ private function processTest(string $testId, Test $test, CompilationContext $con

// check if there are any pipelines in specified environment
if (count($testPipelines) === 0) {
throw new ExerciseConfigException("Exercise configuration does not specify any pipelines for environment '$runtimeEnvironmentId' and test '$testId'");
throw new ExerciseCompilationException("Exercise configuration does not specify any pipelines for environment '$runtimeEnvironmentId' and test '$testId'");
}

// go through all pipelines and merge their data boxes into resulting array
Expand All @@ -321,17 +324,18 @@ private function processTest(string $testId, Test $test, CompilationContext $con
* @param CompilationParams $params
* @return MergeTree[]
* @throws ExerciseConfigException
* @throws ExerciseCompilationException
*/
public function merge(CompilationContext $context, CompilationParams $params): array {
if (count($context->getExerciseConfig()->getTests()) === 0) {
throw new ExerciseConfigException("Exercise configuration does not specify any tests");
throw new ExerciseCompilationException("Exercise configuration does not specify any tests");
}

$tests = array();
foreach ($context->getExerciseConfig()->getTests() as $testId => $test) {
// find test identification in tests names array and retrieve test name
if (!array_key_exists($testId, $context->getTestsNames())) {
throw new ExerciseConfigException("Test with id '{$testId}' does not exist in exercise.");
throw new ExerciseCompilationException("Test with id '{$testId}' does not exist in exercise.");
}
$testName = $context->getTestsNames()[$testId];

Expand Down
Loading

0 comments on commit f192a88

Please sign in to comment.