Add an InternalError type that piggybacks ValidationError

This commit is contained in:
Nabeel Shahzad
2018-03-23 17:29:54 -05:00
parent f5c492bb7e
commit 33daaf4a35
2 changed files with 44 additions and 41 deletions

View File

@@ -0,0 +1,32 @@
<?php
namespace App\Exceptions;
use Illuminate\Validation\ValidationException;
use Validator;
use Log;
/**
* Show an internal error, bug piggyback off of the validation
* exception type - this has a place to show up in the UI as a
* flash message.
* @package App\Exceptions
*/
class InternalError extends ValidationException
{
protected const FLASH_FIELD_NAME = 'internal_error_message';
/**
* InternalError constructor.
* @param string|null $message
* @param null $field
*/
final public function __construct(string $message = null, $field = null)
{
Log::error($message);
$validator = Validator::make([], []);
$validator->errors()->add($field ?? static::FLASH_FIELD_NAME, $message);
parent::__construct($validator);
}
}

View File

@@ -36,6 +36,8 @@ class ImportService extends Service
}
/**
* Throw a validation error back up because it will automatically show
* itself under the CSV file upload, and nothing special needs to be done
* @param $error
* @param $e
* @throws ValidationException
@@ -72,13 +74,15 @@ class ImportService extends Service
/**
* Run the actual importer, pass in one of the Import classes which implements
* the ImportExport interface
* @param Reader $reader
* @param $file_path
* @param ImportExport $importer
* @return array
* @throws ValidationException
*/
protected function runImport(Reader $reader, ImportExport $importer): array
protected function runImport($file_path, ImportExport $importer): array
{
$reader = $this->openCsv($file_path);
$cols = $importer->getColumns();
$first_header = $cols[0];
@@ -126,13 +130,8 @@ class ImportService extends Service
# TODO: delete airports
}
$reader = $this->openCsv($csv_file);
if (!$reader) {
return false;
}
$importer = new AircraftImporter();
return $this->runImport($reader, $importer);
return $this->runImport($csv_file, $importer);
}
/**
@@ -148,13 +147,8 @@ class ImportService extends Service
Airport::truncate();
}
$reader = $this->openCsv($csv_file);
if (!$reader) {
return false;
}
$importer = new AirportImporter();
return $this->runImport($reader, $importer);
return $this->runImport($csv_file, $importer);
}
/**
@@ -170,13 +164,8 @@ class ImportService extends Service
Expense::truncate();
}
$reader = $this->openCsv($csv_file);
if (!$reader) {
return false;
}
$importer = new ExpenseImporter();
return $this->runImport($reader, $importer);
return $this->runImport($csv_file, $importer);
}
/**
@@ -192,14 +181,8 @@ class ImportService extends Service
# TODO: Delete all from: fares
}
$reader = $this->openCsv($csv_file);
if (!$reader) {
# TODO: Throw an error
return false;
}
$importer = new FareImporter();
return $this->runImport($reader, $importer);
return $this->runImport($csv_file, $importer);
}
/**
@@ -215,14 +198,8 @@ class ImportService extends Service
# TODO: Delete all from: flights, flight_field_values
}
$reader = $this->openCsv($csv_file);
if (!$reader) {
# TODO: Throw an error
return false;
}
$importer = new FlightImporter();
return $this->runImport($reader, $importer);
return $this->runImport($csv_file, $importer);
}
/**
@@ -238,13 +215,7 @@ class ImportService extends Service
# TODO: Cleanup subfleet data
}
$reader = $this->openCsv($csv_file);
if (!$reader) {
# TODO: Throw an error
return false;
}
$importer = new SubfleetImporter();
return $this->runImport($reader, $importer);
return $this->runImport($csv_file, $importer);
}
}