Skip to content

Custom Error Messages

Chris Carlevato edited this page Apr 13, 2019 · 2 revisions

When adding support for custom shortener APIs you will also want to add support for custom error messages to provide feedback to users in the event of an error. The utmdc_error_message API filter allows for displaying additional messages in response to custom error codes generated by your shortener class.

Default Error Codes

The following error codes are utilized by default. Using different codes for your own error handling will help prevent confusion.

Error Code Message
1 Invalid URL string provided by the user.
2 Invalid Post ID in the submission.
1000 Error with Shorten object: Not an instance of \UtmDotCodes\Shorten, or an Exception was raise.
100 Bitly: Unable to connect to API.
4030 Bitly: Unauthorized response from API.
500 Bitly: Error response from API.
401 Rebrandly: Unauthorized response from API.
4031 Rebrandly: Error response from API.

Filter Tag: utmdc_error_message

When processing a request with a custom shorten object you can set the error_code property of the object for retrieval via the get_error() method. This error code allows utm.codes to add a query arg for the value to display the appropriate message to the user. The utmdc_error_message filter allows you to include additional messages for your own custom error codes.

The filter sets the style and contents for the error message. The notice class will be added by utm.codes automatically, you can optionally add any of the type specific classes in your filter to adjust the display.

  • Red: notice-error
  • Yellow: notice-warning
  • Green: notice-success
  • Blue: notice-info

Example Usage

add_filter(
	'utmdc_error_message',
	function( $error_message, $error_code ) {
		if ( YOUR_ERROR_CODE_VALUE === $error_code ) {
			$error_message = [
				'style'   => 'notice-TYPE',
				'message' => 'YOUR ERROR MESSAGE',
			];
		}
		return $error_message;
	},
	10,
	2
);