Skip to content

Commit 4f0d980

Browse files
Update to display parent exception
1 parent b9478cb commit 4f0d980

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

lib/Service/ServiceAdminManager.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ public function registerService(ServiceInformations $infos): void
4848
$result = win32_create_service($infos->toArray(), $infos->machine());
4949
} catch (\Win32ServiceException $e) {
5050
$result = $e->getCode();
51+
$errorMessage = $e->getMessage();
5152
}
52-
$this->checkResponseAndConvertInExceptionIfNeed($result, $infos);
53+
$this->checkResponseAndConvertInExceptionIfNeed($result, $infos, $errorMessage ?? '');
5354
$this->throwExceptionIfError(
5455
$result,
5556
ServiceRegistrationException::class,
56-
'Error occured during registration service'
57+
'Error occured during registration service. '.($errorMessage ?? '')
5758
);
5859
}
5960

@@ -77,15 +78,16 @@ public function unregisterService(ServiceIdentificator $infos): void
7778
$result = win32_delete_service($infos->serviceId(), $infos->machine());
7879
} catch (\Win32ServiceException $e) {
7980
$result = $e->getCode();
81+
$errorMessage = $e->getMessage();
8082
}
81-
$this->checkResponseAndConvertInExceptionIfNeed($result, $infos);
83+
$this->checkResponseAndConvertInExceptionIfNeed($result, $infos, $errorMessage ?? '');
8284
if ($result === WIN32_ERROR_SERVICE_MARKED_FOR_DELETE) {
8385
throw new ServiceMarkedForDeleteException('The service is marked for delete. Please reboot the computer.', $result);
8486
}
8587
$this->throwExceptionIfError(
8688
$result,
8789
ServiceUnregistrationException::class,
88-
'Error occured during unregistration service'
90+
'Error occured during unregistration service. '.($errorMessage ?? '')
8991
);
9092
}
9193
}

lib/Service/ServiceInformationsTrait.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ protected function getServiceInformations(ServiceIdentificator $service): Servic
4848
$infos = win32_query_service_status($service->serviceId(), $service->machine());
4949
} catch (\Win32ServiceException $e) {
5050
$infos = $e->getCode();
51+
$messageDetail = $e->getMessage();
5152
}
52-
$this->checkResponseAndConvertInExceptionIfNeed(\is_array($infos) ? null : $infos, $service);
53+
$this->checkResponseAndConvertInExceptionIfNeed(\is_array($infos) ? null : $infos, $service, $messageDetail ?? null);
5354

5455
if (!\is_array($infos)) {
5556
throw new ServiceStatusException('Error on read service status', $infos);
@@ -62,13 +63,13 @@ protected function getServiceInformations(ServiceIdentificator $service): Servic
6263
* @throws ServiceAccessDeniedException
6364
* @throws ServiceNotFoundException
6465
*/
65-
protected function checkResponseAndConvertInExceptionIfNeed(?int $value, ServiceIdentificator $service): void
66+
protected function checkResponseAndConvertInExceptionIfNeed(?int $value, ServiceIdentificator $service, ?string $messageDetail = null): void
6667
{
6768
if ($value === WIN32_ERROR_SERVICE_DOES_NOT_EXIST) {
68-
throw new ServiceNotFoundException('Service ' . $service->serviceId() . ' is not found');
69+
throw new ServiceNotFoundException('Service ' . $service->serviceId() . ' is not found. '.$messageDetail);
6970
}
7071
if ($value === WIN32_ERROR_ACCESS_DENIED) {
71-
throw new ServiceAccessDeniedException('Access to service ' . $service->serviceId() . ' is denied');
72+
throw new ServiceAccessDeniedException('Access to service ' . $service->serviceId() . ' is denied. '.$messageDetail);
7273
}
7374
}
7475

lib/Service/ServiceStateManager.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ public function sendCustomControl(ServiceIdentificator $serviceId, int $control)
8181
$result = win32_send_custom_control($serviceId->serviceId(), $serviceId->machine(), $control);
8282
} catch (\Win32ServiceException $e) {
8383
$result = $e->getCode();
84+
$errorMessage = $e->getMessage();
8485
}
85-
$this->checkResponseAndConvertInExceptionIfNeed($result, $serviceId);
86+
$this->checkResponseAndConvertInExceptionIfNeed($result, $serviceId, $errorMessage ?? '');
8687
$this->throwExceptionIfError(
8788
$result,
8889
Win32ServiceException::class,
@@ -111,19 +112,24 @@ private function actionForService(ServiceIdentificator $serviceId, string $actio
111112
throw new InvalidServiceStatusException(sprintf('The service is not %s', $check));
112113
}
113114

114-
$result = match ($action) {
115-
'start' => win32_start_service($serviceId->serviceId(), $serviceId->machine()),
116-
'stop' => win32_stop_service($serviceId->serviceId(), $serviceId->machine()),
117-
'pause' => win32_pause_service($serviceId->serviceId(), $serviceId->machine()),
118-
'continue' => win32_continue_service($serviceId->serviceId(), $serviceId->machine()),
119-
default => throw new ServiceStateActionException(sprintf('Action "%s" for service is unknown', $action)),
120-
};
115+
try {
116+
$result = match ($action) {
117+
'start' => win32_start_service($serviceId->serviceId(), $serviceId->machine()),
118+
'stop' => win32_stop_service($serviceId->serviceId(), $serviceId->machine()),
119+
'pause' => win32_pause_service($serviceId->serviceId(), $serviceId->machine()),
120+
'continue' => win32_continue_service($serviceId->serviceId(), $serviceId->machine()),
121+
default => throw new ServiceStateActionException(sprintf('Action "%s" for service is unknown', $action)),
122+
};
123+
} catch (\Win32ServiceException $e) {
124+
$result = $e->getCode();
125+
$errorMessage = $e->getMessage();
126+
}
121127

122-
$this->checkResponseAndConvertInExceptionIfNeed($result, $serviceId);
128+
$this->checkResponseAndConvertInExceptionIfNeed($result, $serviceId, $errorMessage ?? null);
123129
$this->throwExceptionIfError(
124130
$result,
125131
ServiceStateActionException::class,
126-
sprintf('Unable to %s service', $action)
132+
sprintf('Unable to %s service. %s', $action, $errorMessage ?? '')
127133
);
128134
}
129135
}

0 commit comments

Comments
 (0)