diff --git a/app/Course/Requests/StoreRequest.php b/app/Course/Requests/StoreRequest.php index b6ad0fee..70597150 100644 --- a/app/Course/Requests/StoreRequest.php +++ b/app/Course/Requests/StoreRequest.php @@ -6,6 +6,8 @@ use App\Course\Models\Course; use App\Member\Member; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Support\Arr; +use Illuminate\Validation\ValidationException; +use Zoomyboy\LaravelNami\NamiException; class StoreRequest extends FormRequest { @@ -36,12 +38,17 @@ class StoreRequest extends FormRequest public function persist(Member $member): void { - $course = Course::findOrFail($this->input('course_id')); + $course = Course::where('id', $this->input('course_id'))->firstOrFail(); $payload = array_merge( $this->only(['event_name', 'completed_at', 'course_id', 'organizer']), ['course_id' => $course->nami_id], ); - $namiId = auth()->user()->api()->createCourse($member->nami_id, $payload); + + try { + $namiId = auth()->user()->api()->createCourse($member->nami_id, $payload); + } catch(NamiException $e) { + throw ValidationException::withMessages(['id' => 'Unbekannter Fehler']); + } $member->courses()->attach( $course, diff --git a/packages/laravel-nami b/packages/laravel-nami index 90d91088..12383090 160000 --- a/packages/laravel-nami +++ b/packages/laravel-nami @@ -1 +1 @@ -Subproject commit 90d91088a983107de634e55b4e24146923c99bed +Subproject commit 1238309006a18aa1de37bbaac74c3089c128c582 diff --git a/tests/Feature/Course/StoreTest.php b/tests/Feature/Course/StoreTest.php index 1d741171..f7dbb980 100644 --- a/tests/Feature/Course/StoreTest.php +++ b/tests/Feature/Course/StoreTest.php @@ -101,4 +101,22 @@ class StoreTest extends TestCase ]); } + public function testItReceivesUnknownErrors(): void + { + $this->login()->init(); + $member = Member::factory()->defaults()->inNami(123)->createOne(); + $course = Course::factory()->inNami(456)->createOne(); + app(CourseFake::class)->doesntCreateWithError(123); + + $response = $this->post("/member/{$member->id}/course", [ + 'course_id' => $course->id, + 'completed_at' => '2021-01-02', + 'event_name' => '::event::', + 'organizer' => '::org::', + ]); + + $response->assertSessionHasErrors(['id' => 'Unbekannter Fehler']); + $this->assertDatabaseCount('course_member', 0); + } + }