/** * Mask actual exception for security reasons in case when it should not be exposed to API clients. * * Convert any exception into \Magento\Framework\Webapi\Exception. * * @param \Exception $exception Exception to convert to a WebAPI exception * * @return WebapiException */ public function maskException(\Exception $exception) { $isDevMode = $this->_appState->getMode() === State::MODE_DEVELOPER; $stackTrace = $isDevMode ? $exception->getTraceAsString() : null; if ($exception instanceof LocalizedException) { // Map HTTP codes for LocalizedExceptions according to exception type if ($exception instanceof NoSuchEntityException) { $httpCode = WebapiException::HTTP_NOT_FOUND; } elseif ($exception instanceof AuthorizationException || $exception instanceof AuthenticationException) { $httpCode = WebapiException::HTTP_UNAUTHORIZED; } else { // Input, Expired, InvalidState exceptions will fall to here $httpCode = WebapiException::HTTP_BAD_REQUEST; } if ($exception instanceof AbstractAggregateException) { $errors = $exception->getErrors(); } else { $errors = null; } $maskedException = new WebapiException($exception->getRawMessage(), $exception->getCode(), $httpCode, $exception->getParameters(), get_class($exception), $errors, $stackTrace); } elseif ($exception instanceof WebapiException) { $maskedException = $exception; } else { $message = $exception->getMessage(); $code = $exception->getCode(); //if not in Dev mode, make sure the message and code is masked for unanticipated exceptions if (!$isDevMode) { /** Log information about actual exception */ $reportId = $this->_critical($exception); $message = sprintf(self::INTERNAL_SERVER_ERROR_MSG, $reportId); $code = 0; } $maskedException = new WebapiException($message, $code, WebapiException::HTTP_INTERNAL_ERROR, [], '', null, $stackTrace); } return $maskedException; }
/** * @param \Magento\Framework\App\RequestInterface $request * @param Server $soapServer * @param \Magento\Framework\Webapi\Exception $exception * @param \Magento\Framework\Locale\ResolverInterface $localeResolver * @param State $appState */ public function __construct(\Magento\Framework\App\RequestInterface $request, Server $soapServer, \Magento\Framework\Webapi\Exception $exception, \Magento\Framework\Locale\ResolverInterface $localeResolver, State $appState) { $this->_soapCode = $exception->getOriginator(); $this->_parameters = $exception->getDetails(); $this->_wrappedErrors = $exception->getErrors(); $this->stackTrace = $exception->getStackTrace() ?: $exception->getTraceAsString(); $this->message = $exception->getMessage(); $this->_request = $request; $this->_soapServer = $soapServer; $this->_localeResolver = $localeResolver; $this->appState = $appState; }