Skip to content

Commit

Permalink
Adds severity (replaces get_code_as_string()).
Browse files Browse the repository at this point in the history
Changes an exceptional error to a warning, and adds new warning.

Changes get_prp_previous() to get_previous(), and changes its access from public to private.

USes exception to handle warning in Genertae_Output about displaying the meta info without the summary.
  • Loading branch information
pandammonium committed Mar 21, 2023
1 parent 039027a commit 39542e1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 31 deletions.
67 changes: 42 additions & 25 deletions classes/exception_prp.php
Expand Up @@ -31,8 +31,9 @@ class PRP_Exception extends Exception {
public const PRP_ERROR_BAD_URL = 202;
public const PRP_ERROR_BAD_DATA = 203;
public const PRP_ERROR_BAD_CACHE = 204;
public const PRP_ERROR_BAD_CALL = 300;
public const PRP_ERROR_DEPRECATED = 400;
public const PRP_WARNING_BAD_CALL = 300;
public const PRP_WARNING_BAD_DATA = 503;
protected $code;

/**
Expand All @@ -52,16 +53,16 @@ private function set_code( $code ): void {
case self::PRP_ERROR_BAD_URL:
case self::PRP_ERROR_BAD_DATA:
case self::PRP_ERROR_BAD_CACHE:
case self::PRP_ERROR_BAD_CALL:
case self::PRP_WARNING_BAD_CALL:
case self::PRP_WARNING_BAD_DATA:
$this->code = $code;
break;
case self::PRP_ERROR_NONE:
$this->code = $code;
throw new InvalidArgumentException( 'Code ' . $code . ' indicates there is no ' . __CLASS__ . ' error' );
break;
case self::PRP_ERROR_BAD_CALL:
case E_USER_WARNING:
$this->code = self::PRP_ERROR_BAD_CALL;
$this->code = self::PRP_WARNING_BAD_CALL;
trigger_error( plugin_readme_parser_name . ': ' . wp_strip_all_tags( $this->get_prp_message() ), E_USER_WARNING );
break;
case self::PRP_ERROR_DEPRECATED:
Expand All @@ -80,28 +81,29 @@ private function get_code(): int {
return $this->code;
}

public static function get_code_as_string( int $code ): string {
private static function get_severity( int $code ): string {
$severity = '';
switch( $code ) {
case self::PRP_ERROR_UNKNOWN:
return 'Unknown error';
case self::PRP_ERROR_BAD_INPUT:
return 'Bad input';
case self::PRP_ERROR_BAD_FILE:
return 'Bad file';
case self::PRP_ERROR_BAD_URL:
return 'Bad URL';
case self::PRP_ERROR_BAD_DATA:
return 'Bad data';
case self::PRP_ERROR_BAD_CACHE:
return 'Bad cache';
case self::PRP_ERROR_DEPRECATED:
return 'Deprecated';
$severity = 'Error';
break;
case self::PRP_WARNING_BAD_CALL:
case self::PRP_WARNING_BAD_DATA:
$severity = 'Warning';
break;
case self::PRP_ERROR_NONE:
return 'None';
$severity = '';
default:
throw new InvalidArgumentException( 'Invalid error code used in ' . __CLASS__ );
throw new InvalidArgumentException( 'Invalid error code used in ' . __CLASS__ . ': ' . $code );
break;
}
return strtoupper( $severity ) . ' ';
}

/**
Expand Down Expand Up @@ -183,7 +185,7 @@ public function get_prp_trace_as_string(): string {
*
* @since 2.0.0
*/
public function get_prp_previous(): ?Throwable {
private function get_previous(): ?Throwable {
return parent::getPrevious();
}

Expand All @@ -202,21 +204,36 @@ public function __prp_to_string(): string {
*/
public function get_prp_nice_error(): string {

$this_msg = self::PRP_PREFIX .
self::get_severity( $this->get_prp_code() ) .
print_r( $this->get_code(), true ) .
" " . print_r( $this->get_prp_message_stripped_of_tags(), true );
$this_loc = self::PRP_PREFIX .
"in " . print_r( $this->get_prp_file() .
" on line " . print_r( $this->get_prp_line(), true ), true );

if ( ( defined( 'WP_DEBUG' ) && WP_DEBUG ) &&
( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) ) {
error_log( self::PRP_PREFIX .
"ERROR " . print_r( $this->get_code(), true ) .
" " . print_r( $this->get_prp_message_stripped_of_tags(), true ) );
error_log( self::PRP_PREFIX .
"in " . print_r( $this->get_prp_file() .
" on line " . print_r( $this->get_prp_line(), true ), true ) );
error_log( $this_msg );
error_log( $this_loc );
$previous = $this->get_previous();
if ( $previous ) {
$previous_msg = self::PRP_PREFIX .
self::get_severity( $previous->get_prp_code() ) . print_r( $previous->get_code(), true ) .
" " . print_r( $previous->get_prp_message_stripped_of_tags(), true );
$previous_loc = self::PRP_PREFIX .
"in " . print_r( $previous->get_prp_file() .
" on line " . print_r( $previous->get_prp_line(), true ), true );
error_log( $previous_msg );
error_log( $previous_loc );
}
}

$display = '<p><span class="error">' . plugin_readme_parser_name . '</span>: ' . print_r( 'ERROR ' . $this->get_prp_code(), true ) . ' ' . print_r( $this->get_prp_message(), true ) . '.</p>';
$previous = $this->get_prp_previous();
$display = '<p><span class="error">' . plugin_readme_parser_name . '</span>: ' . print_r( self::get_severity( $this->get_prp_code() ) . $this->get_prp_code(), true ) . ' ' . print_r( $this->get_prp_message(), true ) . '.</p>';
$previous = $this->get_previous();
if ( $previous ) {
$display .= '<p><span class="error">' . plugin_readme_parser_name . '</span>: ' . print_r( 'ERROR ' . $previous->get_prp_code(), true ) . ' ' . print_r( $previous->get_prp_message(), true ) . '.</p>';
}
$display .= '<p><span class="error">' . plugin_readme_parser_name . '</span>: ' . print_r( self::get_severity( $previous->get_prp_code() ) . $previous->get_prp_code(), true ) . ' ' . print_r( $previous->get_prp_message(), true ) . '.</p>';
}
$delim = ':';
$pos = strpos( $display, $delim );
if ( false !== $pos ) {
Expand Down
16 changes: 10 additions & 6 deletions classes/generate-output.php
Expand Up @@ -803,12 +803,16 @@ private function determine_show_head(): void {

if ( !$this->head_explicitly_excluded ) {
if ( !$this->meta_explicitly_excluded ) {
if ( $this->meta_explicitly_included ) {
$new_include = str_replace( 'meta', 'head', $this->include );
prp_log( __( "Cannot include the metadata part of the head without the summary part:\n Shortcode parameters supplied: include=\"" . $this->include . "\"\n Shortcode parameters changed to: include=\"" . $new_include . "\"", plugin_readme_parser_domain ), "", false, true );
// Swap the `meta` value of the `include` parameter to `head`:
$this->include = $new_include;
// Set the head to be shown as if it had been included in the first place (in the outer `if` statement):
try {
if ( $this->meta_explicitly_included ) {
// The meta section of the head cannot be included without the description. Swap `meta` for `head` in `$this->include` in such a way that the old and new can be output in a message to the user:
$old_include = $this->include;
$new_include = str_replace( 'meta', 'head', $old_include );
$this->include = $new_include;
throw new PRP_Exception( "Cannot include the metadata part of the head without the summary part:\n Shortcode parameters supplied: include=\"" . $old_include . "\"\n Shortcode parameters changed to: include=\"" . $new_include . "\"", PRP_Exception::PRP_WARNING_BAD_CALL );
}
} catch ( PRP_Exception $e ) {
$e->get_prp_nice_error();
}
$this->show_head = true;
$this->show_meta = true;
Expand Down

0 comments on commit 39542e1

Please sign in to comment.