Skip to content

Commit

Permalink
Merge pull request #23 from dschoenbauer/feature/22-add-exception-mes…
Browse files Browse the repository at this point in the history
…sages

Feature/22 add exception messages
  • Loading branch information
dschoenbauer committed Aug 24, 2017
2 parents 3947a7c + 2f61f98 commit a41de0e
Show file tree
Hide file tree
Showing 39 changed files with 689 additions and 293 deletions.
372 changes: 183 additions & 189 deletions composer.lock

Large diffs are not rendered by default.

86 changes: 86 additions & 0 deletions src/Exception/Enum/ExceptionDefaultMessages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/*
* The MIT License
*
* Copyright 2017 David Schoenbauer <dschoenbauer@gmail.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
namespace DSchoenbauer\Exception\Enum;

/**
* A group of constants that house their default messages
* Tips for a good error message:
* 1. Be specific to the user’s task. Provide guidance.
* 2. No one ever died of humility. Remember that we (the service) are human too.
* 3. Embrace humor in the situation. Keep it polite, and light hearted. Cute is great but informative is better
* 4. Don’t make users work for it. Provide help and hints to getting the issues resolved.
* 5. Error messaging is customer support.
*
* @author David Schoenbauer
*/
class ExceptionDefaultMessages
{

//Client Error Exceptions
const CLIENT_ERROR_EXCEPTION = "An error has occured. Please modify content and resubmit your request.";
const BAD_REQUEST_EXCEPTION = "Your browser sent a request that this server could not understand.";
const RUNTIME_EXCEPTION = "This application has requested the Runtime to terminate it in an unusual way.";
const CONFLICT_EXCEPTION = "The request could not be completed due to a conflict with the current state of the "
. "target resource.";
const FORBIDDEN_EXCEPTION = "The server understood the request but refuses to authorize it.";
const NOT_ALLOWED_EXCEPTION = "The method received in the request-line is known by the origin server but not "
. "supported by the target resource.";
const NOT_FOUND_EXCEPTION = "The origin server did not find a current representation for the target resource or is "
. "not willing to disclose that one exists.";
const PAYLOAD_TOO_LARGE_EXCEPTION = "The server is refusing to process a request because the request payload is "
. "larger than the server is willing or able to process.";
const PAYMENT_REQUIRED_EXCEPTION = "To continue a payment is required.";
const UNAUTHORIZED_EXCEPTION = "The request has not been applied because it lacks valid authentication credentials "
. "for the target resource.";
const UNAVAILABLE_FOR_LEGAL_REASONS_EXCEPTION = "The server is denying access to the resource as a consequence of "
. "a legal demand.";
const UNPROCESSABLE_ENTITY_EXCEPTION = "The server understands the content type of the request entity, and the "
. "syntax of the request entity is correct but was unable to process the contained instructions.";
const UNSUPPORTED_MEDIA_TYPE_EXCEPTION = "The origin server is refusing to service the request because the payload "
. "is in a format not supported by this method on the target resource.";
const UPGRADE_REQUIRED_EXCEPTION = "The server refuses to perform the request using the current protocol but "
. "might be willing to do so after the client upgrades to a different protocol.";
//Server Errors
const INTERNAL_SERVER_ERROR_EXCEPTION = "The server encountered an unexpected condition which prevented it from "
. "fulfilling the request.";
const SERVER_ERROR_EXCEPTION = "An error has occurred on the server. Please be patient why techs repair the issue.";
const SERVICE_UNAVAILABLE_EXCEPTION = "The server is currently unable to handle the request due to a temporary "
. "overload or scheduled maintenance, which will likely be alleviated after some delay.";
//Plateform Errors:
const BAD_FUNCTION_CALL_EXCEPTION = "A callback refers to an undefined function or if some arguments are missing.";
const BAD_METHOD_CALL_EXCEPTION = "A callback refers to an undefined method or if some arguments are missing.";
const DOMAIN_EXCEPTION = "A value provided does not part of the required domain.";
const EXCEPTION = "An error has occured.";
const INVALID_ARGUMENT_EXCEPTION = "An argument is not of the expected type.";
const LENGTH_EXCEPTION = "The length is invalid.";
const LOGIC_EXCEPTION = "There is an error in the program logic.";
const OUT_OF_BOUNDS_EXCEPTION = "A value is not a valid key.";
const OUT_OF_RANGE_EXCEPTION = "An illegal index was requested.";
const OVERFLOW_EXCEPTION = "Container is full.";
const RANGE_EXCEPTION = "A range error has occured.";
const UNDERFLOW_EXCEPTION = "An invalid operation hos been attempted on an empty container.";
const UNEXPECTED_VALUE_EXCEPTION = "A value does not match with a set of values.";

}
5 changes: 2 additions & 3 deletions src/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/*
* The MIT License
*
Expand All @@ -23,7 +22,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

namespace DSchoenbauer\Exception;

/**
Expand All @@ -35,5 +33,6 @@
*/
interface ExceptionInterface
{


public function getDefaultMessage();
}
14 changes: 11 additions & 3 deletions src/Exception/Http/ClientError/BadRequestException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/*
* The MIT License
*
Expand All @@ -23,9 +22,10 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

namespace DSchoenbauer\Exception\Http\ClientError;

use DSchoenbauer\Exception\Enum\ExceptionDefaultMessages;

/**
* The server cannot or will not process the request due to something that is
* perceived to be a client error (e.g., malformed request syntax, invalid
Expand All @@ -37,8 +37,16 @@
class BadRequestException extends ClientErrorException
{

public function __construct($message = "")
public function __construct($message = null)
{
if (!$message) {
$message = $this->getDefaultMessage();
}
parent::__construct($message, 400);
}

public function getDefaultMessage()
{
return ExceptionDefaultMessages::BAD_REQUEST_EXCEPTION;
}
}
8 changes: 6 additions & 2 deletions src/Exception/Http/ClientError/ClientErrorException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/*
* The MIT License
*
Expand All @@ -23,9 +22,9 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

namespace DSchoenbauer\Exception\Http\ClientError;

use DSchoenbauer\Exception\Enum\ExceptionDefaultMessages;
use DSchoenbauer\Exception\Http\HttpExceptionInterface;
use DSchoenbauer\Exception\Platform\RuntimeException;

Expand All @@ -37,4 +36,9 @@
*/
class ClientErrorException extends RuntimeException implements HttpExceptionInterface
{

public function getDefaultMessage()
{
return ExceptionDefaultMessages::CLIENT_ERROR_EXCEPTION;
}
}
9 changes: 7 additions & 2 deletions src/Exception/Http/ClientError/ConflictException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/*
* The MIT License
*
Expand All @@ -23,9 +22,10 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

namespace DSchoenbauer\Exception\Http\ClientError;

use DSchoenbauer\Exception\Enum\ExceptionDefaultMessages;

/**
* The request could not be completed due to a conflict with the current state
* of the target resource. This code is used in situations where the user might
Expand Down Expand Up @@ -53,4 +53,9 @@ public function __construct($reasonForError = "")
{
parent::__construct($reasonForError, 409);
}

public function getDefaultMessage()
{
return ExceptionDefaultMessages::CONFLICT_EXCEPTION;
}
}
9 changes: 7 additions & 2 deletions src/Exception/Http/ClientError/ForbiddenException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/*
* The MIT License
*
Expand All @@ -23,9 +22,10 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

namespace DSchoenbauer\Exception\Http\ClientError;

use DSchoenbauer\Exception\Enum\ExceptionDefaultMessages;

/**
* The server understood the request but refuses to authorize it.
*
Expand All @@ -52,4 +52,9 @@ public function __construct($message = "")
{
parent::__construct($message, 403);
}

public function getDefaultMessage()
{
return ExceptionDefaultMessages::FORBIDDEN_EXCEPTION;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php


/*
* The MIT License
*
Expand All @@ -24,8 +22,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


namespace DSchoenbauer\Exception\Http\ClientError;

/**
Expand All @@ -49,4 +45,9 @@ public function __construct($message = "")
{
parent::__construct($message, 405);
}

public function getDefaultMessage()
{
return \DSchoenbauer\Exception\Enum\ExceptionDefaultMessages::NOT_ALLOWED_EXCEPTION;
}
}
7 changes: 5 additions & 2 deletions src/Exception/Http/ClientError/NotFoundException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/*
* The MIT License
*
Expand All @@ -23,7 +22,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

namespace DSchoenbauer\Exception\Http\ClientError;

/**
Expand All @@ -49,4 +47,9 @@ public function __construct($message = "")
{
parent::__construct($message, 404);
}

public function getDefaultMessage()
{
return \DSchoenbauer\Exception\Enum\ExceptionDefaultMessages::NOT_FOUND_EXCEPTION;
}
}
9 changes: 5 additions & 4 deletions src/Exception/Http/ClientError/PayloadTooLargeException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/*
* The MIT License
*
Expand All @@ -23,7 +22,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

namespace DSchoenbauer\Exception\Http\ClientError;

/**
Expand All @@ -46,15 +44,18 @@ class PayloadTooLargeException extends ClientErrorException

private $maxPayloadSize = 0;



public function __construct($maxPayloadSize = 0, $message = "")
{

parent::__construct($message, 413);
$this->setMaxPayloadSize($maxPayloadSize);
}

public function getDefaultMessage()
{
return \DSchoenbauer\Exception\Enum\ExceptionDefaultMessages::PAYLOAD_TOO_LARGE_EXCEPTION;
}

/**
* @return integer representation of max payload size
*/
Expand Down
7 changes: 5 additions & 2 deletions src/Exception/Http/ClientError/PaymentRequiredException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/*
* The MIT License
*
Expand All @@ -23,7 +22,6 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

namespace DSchoenbauer\Exception\Http\ClientError;

/**
Expand All @@ -40,4 +38,9 @@ public function __construct($message = "")
{
parent::__construct($message, 402);
}

public function getDefaultMessage()
{
return \DSchoenbauer\Exception\Enum\ExceptionDefaultMessages::PAYMENT_REQUIRED_EXCEPTION;
}
}
9 changes: 7 additions & 2 deletions src/Exception/Http/ClientError/UnauthorizedException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/*
* The MIT License
*
Expand All @@ -23,9 +22,10 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

namespace DSchoenbauer\Exception\Http\ClientError;

use DSchoenbauer\Exception\Enum\ExceptionDefaultMessages;

/**
* The request has not been applied because it lacks valid authentication
* credentials for the target resource.
Expand All @@ -52,4 +52,9 @@ public function __construct($message = "")
{
parent::__construct($message, 401);
}

public function getDefaultMessage()
{
return ExceptionDefaultMessages::UNAUTHORIZED_EXCEPTION;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/*
* The MIT License
*
Expand All @@ -23,9 +22,10 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

namespace DSchoenbauer\Exception\Http\ClientError;

use DSchoenbauer\Exception\Enum\ExceptionDefaultMessages;

/**
* Intended to be used when resource access is denied for legal reasons, e.g.
* censorship or government-mandated blocked access. A reference to the 1953
Expand Down Expand Up @@ -54,4 +54,9 @@ public function __construct($message = "")
{
parent::__construct($message, 451);
}

public function getDefaultMessage()
{
return ExceptionDefaultMessages::UNAVAILABLE_FOR_LEGAL_REASONS_EXCEPTION;
}
}

0 comments on commit a41de0e

Please sign in to comment.