Check if specific role exists

This is based on Bosco’s comment. You can do this instead of line 3 in the above code:

wp_roles()->is_role( 'editor' );

That grabs a WP_Roles object (what the code you had tries to do by getting a global variable) and calls the same is_role() function.