add some error reporting

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
Arthur Schiwon 2018-05-17 22:38:32 +02:00
parent 7486b7a918
commit d85093a010
No known key found for this signature in database
GPG Key ID: 7424F1874854DF23
4 changed files with 78 additions and 6 deletions

View File

@ -43,14 +43,29 @@
_doRequest($context) {
$context.prop('disabled', 'disabled');
$context.addClass('loading');
$context.siblings('span.warning').addClass('hidden').html('');
$.ajax({
type: 'POST',
url: OC.linkToOCS('apps/data_request/api/v1', 2) + $context.data('request'),
dataType: 'json',
beforeSend: function (request) {
request.setRequestHeader('Accept', 'application/json');
},
success: function () {
$context.html($context.html() + ' ' + t('data_request', 'sent!'));
$context.removeClass('loading');
},
error: function () {
error: function (response) {
$context.prop('disabled', '');
$context.removeClass('loading');
if(response.responseJSON && response.responseJSON.ocs.data.error) {
$context.siblings('span.warning')
.removeClass('hidden')
.html(response.responseJSON.ocs.data.error);
}
}
});
}

View File

@ -23,6 +23,7 @@
namespace OCA\DataRequest\Controller;
use OC\HintException;
use OCA\DataRequest\Services\Request;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataResponse;
@ -70,9 +71,9 @@ class DataRequestController extends OCSController {
try {
$this->dataRequest->sendDeleteRequest();
return new DataResponse();
} catch(\RuntimeException $e) {
} catch(HintException $e) {
return new DataResponse(
['error' => $e->getMessage()],
['error' => $e->getHint()],
Http::STATUS_INTERNAL_SERVER_ERROR
);
}

View File

@ -0,0 +1,45 @@
<?php
/**
* @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\DataRequest\Exceptions;
use Throwable;
class HintedRuntime extends \RuntimeException {
/** @var string */
protected $hint;
public function __construct($message = '', $hint = '', $code = 0, Throwable $previous = null) {
parent::__construct($message, $code, $previous);
$this->hint = $hint;
}
public function getHint() {
if (empty($this->hint)) {
return $this->message;
}
return $this->hint;
}
}

View File

@ -23,9 +23,11 @@
namespace OCA\DataRequest\Services;
use OCA\DataRequest\Exceptions\HintedRuntime;
use OCP\Defaults;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IL10N;
use OCP\IUser;
use OCP\IUserSession;
use OCP\L10N\IFactory;
@ -46,13 +48,16 @@ class Request {
private $config;
/** @var IUser */
private $requester;
/** @var IL10N */
private $l;
public function __construct(IGroupManager $groupManager, IMailer $mailer, IFactory $l10nFactory, IConfig $config, IUserSession $userSession) {
public function __construct(IGroupManager $groupManager, IMailer $mailer, IFactory $l10nFactory, IConfig $config, IUserSession $userSession, IL10N $l) {
$this->groupManager = $groupManager;
$this->mailer = $mailer;
$this->l10nFactory = $l10nFactory;
$this->config = $config;
$this->requester = $userSession->getUser();
$this->l = $l;
}
public function sendExportRequest() {
@ -74,7 +79,10 @@ class Request {
}
}
if(!$oneMailSent) {
throw new \RuntimeException('No mail was sent successfully');
throw new HintedRuntime(
'No mail was sent successfully',
$this->l->t('No administrator could have been contacted.')
);
}
}
@ -143,7 +151,10 @@ class Request {
return $admin->getEMailAddress() !== null;
});
if(empty($admins)) {
throw new \RuntimeException('No admin has entered an email address');
throw new HintedRuntime(
'No admin has entered an email address',
$this->l->t('No administrator has set an email address')
);
}
return $admins;
}