diff --git a/app/Services/DawaiService.php b/app/Services/DawaiService.php index 3f3b51f2..2f8e45ef 100644 --- a/app/Services/DawaiService.php +++ b/app/Services/DawaiService.php @@ -9,6 +9,7 @@ use Carbon\Carbon; use Error; use GuzzleHttp\Client; use GuzzleHttp\Exception\ClientException; +use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\ServerException; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Log; @@ -40,6 +41,30 @@ class DawaiService : null; } + /** + * Pesan error ringkas (satu baris, maks 200 karakter) untuk log dan exception. + * Pesan bawaan Guzzle memuat potongan body respons (sering HTML) sehingga log menjadi panjang. + */ + private function ringkasError(Throwable $error): string + { + if ($error instanceof RequestException && $error->hasResponse()) { + $response = $error->getResponse(); + $uri = $error->getRequest()->getUri(); + $pesan = 'HTTP '.$response->getStatusCode().' '.$response->getReasonPhrase() + .' - '.$error->getRequest()->getMethod().' '.$uri->getPath().($uri->getQuery() !== '' ? '?'.$uri->getQuery() : ''); + + $body = json_decode((string) $response->getBody(), true); + $detail = is_array($body) ? ($body['meta']['message'] ?? $body['message'] ?? null) : null; + if (is_string($detail) && trim($detail) !== '') { + $pesan .= ': '.$detail; + } + + return Str::limit($pesan, 200); + } + + return Str::limit(trim(preg_replace('/\s+/', ' ', strip_tags($error->getMessage()))), 200); + } + public function __construct($default = 'http://172.16.40.128/') { $this->client = new Client([ @@ -54,7 +79,7 @@ class DawaiService $response = $this->client->get("api/v1/jabatan/$nip/$periode"); } catch (Throwable $th) { try { - Log::error('DAWAI : '.$th->getMessage()); + Log::error('DAWAI : '.$this->ringkasError($th)); Log::error("DAWAI : $nip ($periode)"); } catch (Throwable) { // Log failure should not crash the process @@ -73,7 +98,7 @@ class DawaiService try { $response = $this->client->get("api/v1/listBawahanAtasanv2/$nip"); } catch (Throwable $th) { - Log::error('DAWAI Bawahan: '.$th->getMessage()); + Log::error('DAWAI Bawahan: '.$this->ringkasError($th)); Log::error("DAWAI Bawahan: $nip"); return []; @@ -128,8 +153,8 @@ class DawaiService return $data; } catch (Throwable $th) { - Log::error('SIDIA: '.$th->getMessage()); - throw new Error($th->getMessage(), 500); + Log::error('SIDIA: '.$this->ringkasError($th)); + throw new Error($this->ringkasError($th), 500); } } @@ -170,9 +195,9 @@ class DawaiService return ['success' => false, 'message' => $data['message'] ?? 'Gagal mengubah daftar bawahan']; } catch (Throwable $th) { - Log::error('Update Bawahan Error: '.$th->getMessage()); + Log::error('Update Bawahan Error: '.$this->ringkasError($th)); - return ['success' => false, 'message' => $th->getMessage()]; + return ['success' => false, 'message' => $this->ringkasError($th)]; } } @@ -222,7 +247,7 @@ class DawaiService throw new Error($data['message']); } catch (Throwable $th) { $this->setPresensiError($th); - Log::error('Presensi DAWAI : '.$th->getMessage()); + Log::error('Presensi DAWAI : '.$this->ringkasError($th)); return false; } @@ -307,12 +332,12 @@ class DawaiService } $this->setPresensiError($e); - Log::warning('DAWAI siskaPengajuanPresensi: '.($pesan !== '' ? $pesan : $e->getMessage())); + Log::warning('DAWAI siskaPengajuanPresensi: '.($pesan !== '' ? $pesan : $this->ringkasError($e))); return false; } catch (Throwable $th) { $this->setPresensiError($th); - Log::error('DAWAI siskaPengajuanPresensi: '.$th->getMessage()); + Log::error('DAWAI siskaPengajuanPresensi: '.$this->ringkasError($th)); return false; } @@ -348,8 +373,8 @@ class DawaiService return $data; } catch (Throwable $th) { - Log::error('DAWAI : '.$th->getMessage()); - throw new Error($th->getMessage(), 500); + Log::error('DAWAI : '.$this->ringkasError($th)); + throw new Error($this->ringkasError($th), 500); } } @@ -361,8 +386,8 @@ class DawaiService return $data; } catch (Throwable $th) { - Log::error('DAWAI : '.$th->getMessage()); - throw new Error($th->getMessage(), 500); + Log::error('DAWAI : '.$this->ringkasError($th)); + throw new Error($this->ringkasError($th), 500); } } @@ -380,10 +405,10 @@ class DawaiService : Str::limit(trim(strip_tags($body)), 200); Log::error('DAWAI : listStatusPresensi gagal - server DAWAI error (HTTP '.$th->getResponse()->getStatusCode().') bukan dari Verifikator, perbaiki di sisi DAWAI. Penyebab: '.$penyebab); - throw new Error($th->getMessage(), 500); + throw new Error($this->ringkasError($th), 500); } catch (Throwable $th) { - Log::error('DAWAI : listStatusPresensi gagal - '.$th->getMessage()); - throw new Error($th->getMessage(), 500); + Log::error('DAWAI : listStatusPresensi gagal - '.$this->ringkasError($th)); + throw new Error($this->ringkasError($th), 500); } } @@ -395,8 +420,8 @@ class DawaiService return $data; } catch (Throwable $th) { - Log::error('DAWAI : '.$th->getMessage()); - throw new Error($th->getMessage(), 500); + Log::error('DAWAI : '.$this->ringkasError($th)); + throw new Error($this->ringkasError($th), 500); } } @@ -413,7 +438,7 @@ class DawaiService return $data['status']; } catch (Throwable $th) { - Log::error('DAWAI : '.$th->getMessage()); + Log::error('DAWAI : '.$this->ringkasError($th)); return true; } @@ -445,13 +470,13 @@ class DawaiService return ['berhasil' => true, 'message' => $message, 'data' => $result['data'] ?? null]; } catch (ClientException $e) { $message = json_decode((string) $e->getResponse()->getBody(), true)['meta']['message'] ?? $e->getMessage(); - Log::error("DAWAI deletePengajuanCutiDinas ($id): ".$e->getMessage()); + Log::error("DAWAI deletePengajuanCutiDinas ($id): ".$this->ringkasError($e)); return ['berhasil' => false, 'message' => $message, 'data' => null]; } catch (Throwable $th) { - Log::error("DAWAI deletePengajuanCutiDinas ($id): ".$th->getMessage()); + Log::error("DAWAI deletePengajuanCutiDinas ($id): ".$this->ringkasError($th)); - return ['berhasil' => false, 'message' => $th->getMessage(), 'data' => null]; + return ['berhasil' => false, 'message' => $this->ringkasError($th), 'data' => null]; } } @@ -463,8 +488,8 @@ class DawaiService return $data; } catch (Throwable $th) { - Log::error('DAWAI : '.$th->getMessage()); - throw new Error($th->getMessage(), 500); + Log::error('DAWAI : '.$this->ringkasError($th)); + throw new Error($this->ringkasError($th), 500); } } @@ -475,8 +500,8 @@ class DawaiService return json_decode($response->getBody()->getContents(), true) ?? []; } catch (Throwable $th) { - Log::error('DAWAI : '.$th->getMessage()); - throw new Error($th->getMessage(), 500); + Log::error('DAWAI : '.$this->ringkasError($th)); + throw new Error($this->ringkasError($th), 500); } } @@ -495,8 +520,8 @@ class DawaiService return is_array($data) ? $data : []; } catch (Throwable $th) { - Log::error('DAWAI : '.$th->getMessage()); - throw new Error($th->getMessage(), 500); + Log::error('DAWAI : '.$this->ringkasError($th)); + throw new Error($this->ringkasError($th), 500); } } @@ -515,8 +540,8 @@ class DawaiService return is_array($data) ? $data : []; } catch (Throwable $th) { - Log::error('DAWAI : '.$th->getMessage()); - throw new Error($th->getMessage(), 500); + Log::error('DAWAI : '.$this->ringkasError($th)); + throw new Error($this->ringkasError($th), 500); } } @@ -527,8 +552,8 @@ class DawaiService return json_decode($response->getBody()->getContents(), true) ?? []; } catch (Throwable $th) { - Log::error('DAWAI : '.$th->getMessage()); - throw new Error($th->getMessage(), 500); + Log::error('DAWAI : '.$this->ringkasError($th)); + throw new Error($this->ringkasError($th), 500); } } @@ -546,8 +571,8 @@ class DawaiService return $data; } catch (Throwable $th) { - Log::error('DAWAI : '.$th->getMessage()); - throw new Error($th->getMessage(), 500); + Log::error('DAWAI : '.$this->ringkasError($th)); + throw new Error($this->ringkasError($th), 500); } } @@ -558,8 +583,8 @@ class DawaiService return json_decode($response->getBody()->getContents(), true) ?? []; } catch (Throwable $th) { - Log::error('DAWAI : '.$th->getMessage()); - throw new Error($th->getMessage(), 500); + Log::error('DAWAI : '.$this->ringkasError($th)); + throw new Error($this->ringkasError($th), 500); } } @@ -577,8 +602,8 @@ class DawaiService return $data; } catch (Throwable $th) { - Log::error('DAWAI : '.$th->getMessage()); - throw new Error($th->getMessage(), 500); + Log::error('DAWAI : '.$this->ringkasError($th)); + throw new Error($this->ringkasError($th), 500); } } }