Making WordPress.org

Changeset 7735


Ignore:
Timestamp:
10/17/2018 02:16:12 PM (7 years ago)
Author:
vedjain
Message:

WordCamp: Add handlers to forward errors to Slack.

This register new error handlers that will notify WordCamp devs on slack when an error has occured. We would be able to proactively take action on a bug instead of waiting for someone to report it to us.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordcamp.org/public_html/wp-content/mu-plugins/wcorg-misc.php

    r7727 r7735  
    435435
    436436add_filter( 'map_meta_cap', 'wcorg_central_modify_export_caps', 10, 2 );
     437
     438    /**
     439     * Error handler to send errors to slack.
     440     */
     441function send_error_to_slack( $err_no, $err_msg, $file, $line ) {
     442
     443    $domain = get_site_url();
     444
     445    $page_slug = trim( $_SERVER["REQUEST_URI"] , '/' );
     446
     447    $text = "Error $err_no : \"$err_msg\" occured on \"$file:$line\" \n Domain: $domain \n Page: $page_slug";
     448
     449    $message = array(
     450        "attachments" => array(
     451            array(
     452                "fallback" => $text,
     453                "color" => "#ff0000",
     454                "pretext" => "Error on \"$file:$line\" ",
     455                "author_name" => $domain,
     456                "text" => $text,
     457            ),
     458        ),
     459    );
     460
     461    $data = json_encode( $message );
     462
     463    $req = curl_init( SLACK_ERROR_REPORT_URL );
     464    curl_setopt( $req, CURLOPT_HTTPHEADER, array( "Content-type: application/json" ) );
     465    curl_setopt( $req, CURLOPT_POST, true );
     466    curl_setopt( $req, CURLOPT_POSTFIELDS, $data );
     467
     468    curl_exec( $req );
     469
     470    return false;
     471}
     472
     473    /**
     474     * Shutdown handler which forwards errors to slack.
     475     */
     476function send_fatal_to_slack() {
     477    if( ! $error = error_get_last() ) {
     478        return;
     479    }
     480
     481    return send_error_to_slack( $error['type'], $error['message'], $error['file'], $error['line'] );
     482}
     483
     484if ( ! defined( 'WPORG_SANDBOXED' ) || ! WPORG_SANDBOXED ) {
     485    register_shutdown_function( 'send_fatal_to_slack' );
     486    set_error_handler( 'send_error_to_slack', E_ERROR );
     487}
     488
Note: See TracChangeset for help on using the changeset viewer.