mirror of
				https://github.com/TitanEmbeds/Titan.git
				synced 2025-11-03 23:37:09 +01:00 
			
		
		
		
	Utilize sentry to track execution errors
This commit is contained in:
		@@ -10,4 +10,6 @@ config = {
 | 
				
			|||||||
    'bots-discord-pw-token': "bots.discord.pw Post Stats Token",
 | 
					    'bots-discord-pw-token': "bots.discord.pw Post Stats Token",
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    'logging-location': "/home/titan/Titan/discordbot/titanbot.log",
 | 
					    'logging-location': "/home/titan/Titan/discordbot/titanbot.log",
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    "sentry-dsn": "Copy the dns string when creating a project on sentry",
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -5,6 +5,8 @@ from titanembeds.commands import Commands
 | 
				
			|||||||
from titanembeds.socketio import SocketIOInterface
 | 
					from titanembeds.socketio import SocketIOInterface
 | 
				
			||||||
from titanembeds.poststats import DiscordBotsOrg, BotsDiscordPw
 | 
					from titanembeds.poststats import DiscordBotsOrg, BotsDiscordPw
 | 
				
			||||||
from collections import deque
 | 
					from collections import deque
 | 
				
			||||||
 | 
					from raven import Client as RavenClient
 | 
				
			||||||
 | 
					import raven
 | 
				
			||||||
import discord
 | 
					import discord
 | 
				
			||||||
import aiohttp
 | 
					import aiohttp
 | 
				
			||||||
import asyncio
 | 
					import asyncio
 | 
				
			||||||
@@ -15,6 +17,10 @@ logging.basicConfig(filename='titanbot.log',level=logging.INFO,format='%(asctime
 | 
				
			|||||||
handler = logging.FileHandler(config.get("logging-location", "titanbot.log"))
 | 
					handler = logging.FileHandler(config.get("logging-location", "titanbot.log"))
 | 
				
			||||||
logging.getLogger('TitanBot')
 | 
					logging.getLogger('TitanBot')
 | 
				
			||||||
logging.getLogger('sqlalchemy')
 | 
					logging.getLogger('sqlalchemy')
 | 
				
			||||||
 | 
					try:
 | 
				
			||||||
 | 
					    raven_client = RavenClient(config["sentry-dsn"])
 | 
				
			||||||
 | 
					except raven.exceptions.InvalidDsn:
 | 
				
			||||||
 | 
					    pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Titan(discord.AutoShardedClient):
 | 
					class Titan(discord.AutoShardedClient):
 | 
				
			||||||
    def __init__(self):
 | 
					    def __init__(self):
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,3 +16,5 @@ flask-redis
 | 
				
			|||||||
gino
 | 
					gino
 | 
				
			||||||
sqlalchemy==1.2.8
 | 
					sqlalchemy==1.2.8
 | 
				
			||||||
asyncio_redis
 | 
					asyncio_redis
 | 
				
			||||||
 | 
					raven
 | 
				
			||||||
 | 
					raven[flask]
 | 
				
			||||||
@@ -27,4 +27,7 @@ config = {
 | 
				
			|||||||
    
 | 
					    
 | 
				
			||||||
    # https://titanembeds.com/api/webhook/discordbotsorg/vote
 | 
					    # https://titanembeds.com/api/webhook/discordbotsorg/vote
 | 
				
			||||||
    'discordbotsorg-webhook-secret': "Secret code used in the authorization header for DBL webhook",
 | 
					    'discordbotsorg-webhook-secret': "Secret code used in the authorization header for DBL webhook",
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    # Sentry.io is used to track and upload errors
 | 
				
			||||||
 | 
					    "sentry-dsn": "Copy the dns string when creating a project on sentry",
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -41,6 +41,10 @@ def init_debug():
 | 
				
			|||||||
        except OSError:
 | 
					        except OSError:
 | 
				
			||||||
            return "ERROR"
 | 
					            return "ERROR"
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
 | 
					    @app.route("/error")
 | 
				
			||||||
 | 
					    def make_error():
 | 
				
			||||||
 | 
					        1 / 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return "OK"
 | 
					        return "OK"
 | 
				
			||||||
if __name__ == "__main__":
 | 
					if __name__ == "__main__":
 | 
				
			||||||
    init_debug()
 | 
					    init_debug()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -12,9 +12,9 @@ except:
 | 
				
			|||||||
        monkey.patch_all()
 | 
					        monkey.patch_all()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from .database import db
 | 
					from .database import db
 | 
				
			||||||
from flask import Flask, render_template, request, session, url_for, redirect, jsonify
 | 
					from flask import Flask, render_template, request, session, url_for, redirect, jsonify, g
 | 
				
			||||||
from flask_sslify import SSLify
 | 
					from flask_sslify import SSLify
 | 
				
			||||||
from titanembeds.utils import rate_limiter, discord_api, socketio, babel, redis_store, language_code_list
 | 
					from titanembeds.utils import rate_limiter, discord_api, socketio, babel, redis_store, language_code_list, sentry
 | 
				
			||||||
from .blueprints import api, user, admin, embed, gateway
 | 
					from .blueprints import api, user, admin, embed, gateway
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
from titanembeds.database import get_administrators_list
 | 
					from titanembeds.database import get_administrators_list
 | 
				
			||||||
@@ -34,6 +34,7 @@ app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=3)
 | 
				
			|||||||
app.config['REDIS_URL'] = config["redis-uri"]
 | 
					app.config['REDIS_URL'] = config["redis-uri"]
 | 
				
			||||||
app.secret_key = config['app-secret']
 | 
					app.secret_key = config['app-secret']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					sentry.init_app(app)
 | 
				
			||||||
db.init_app(app)
 | 
					db.init_app(app)
 | 
				
			||||||
rate_limiter.init_app(app)
 | 
					rate_limiter.init_app(app)
 | 
				
			||||||
if config.get("enable-ssl", False):
 | 
					if config.get("enable-ssl", False):
 | 
				
			||||||
@@ -91,3 +92,10 @@ def context_processor():
 | 
				
			|||||||
        "af_mode_enabled": datetime.datetime.now().date() == datetime.date(datetime.datetime.now().year, 4, 1),
 | 
					        "af_mode_enabled": datetime.datetime.now().date() == datetime.date(datetime.datetime.now().year, 4, 1),
 | 
				
			||||||
        "dbl_voted": session.get("unauthenticated", True) == False and bool(redis_store.get("DiscordBotsOrgVoted/" + str(session.get("user_id", -1))))
 | 
					        "dbl_voted": session.get("unauthenticated", True) == False and bool(redis_store.get("DiscordBotsOrgVoted/" + str(session.get("user_id", -1))))
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@app.errorhandler(500)
 | 
				
			||||||
 | 
					def internal_server_error(error):
 | 
				
			||||||
 | 
					    return render_template('500.html.j2',
 | 
				
			||||||
 | 
					        event_id=g.sentry_event_id,
 | 
				
			||||||
 | 
					        public_dsn=sentry.client.get_public_dsn('https')
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
							
								
								
									
										11
									
								
								webapp/titanembeds/templates/500.html.j2
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										11
									
								
								webapp/titanembeds/templates/500.html.j2
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,11 @@
 | 
				
			|||||||
 | 
					<!-- Sentry JS SDK 2.1.+ required -->
 | 
				
			||||||
 | 
					<script src="https://cdn.ravenjs.com/2.3.0/raven.min.js"></script>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					{% if event_id %}
 | 
				
			||||||
 | 
					  <script>
 | 
				
			||||||
 | 
					  Raven.showReportDialog({
 | 
				
			||||||
 | 
					    eventId: '{{ event_id }}',
 | 
				
			||||||
 | 
					    dsn: '{{ public_dsn }}'
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					  </script>
 | 
				
			||||||
 | 
					{% endif %}
 | 
				
			||||||
@@ -7,6 +7,7 @@ from flask_babel import Babel
 | 
				
			|||||||
from flask_redis import FlaskRedis
 | 
					from flask_redis import FlaskRedis
 | 
				
			||||||
from config import config
 | 
					from config import config
 | 
				
			||||||
from sqlalchemy import and_
 | 
					from sqlalchemy import and_
 | 
				
			||||||
 | 
					from raven.contrib.flask import Sentry
 | 
				
			||||||
import random
 | 
					import random
 | 
				
			||||||
import string
 | 
					import string
 | 
				
			||||||
import hashlib
 | 
					import hashlib
 | 
				
			||||||
@@ -352,6 +353,7 @@ def is_int(specimen):
 | 
				
			|||||||
rate_limiter = Limiter(key_func=get_client_ipaddr) # Default limit by ip address
 | 
					rate_limiter = Limiter(key_func=get_client_ipaddr) # Default limit by ip address
 | 
				
			||||||
socketio = SocketIO(engineio_logger=config.get("engineio-logging", False))
 | 
					socketio = SocketIO(engineio_logger=config.get("engineio-logging", False))
 | 
				
			||||||
babel = Babel()
 | 
					babel = Babel()
 | 
				
			||||||
 | 
					sentry = Sentry(dsn=config.get("sentry-dsn", None))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@socketio.on_error_default  # disconnect on all errors
 | 
					@socketio.on_error_default  # disconnect on all errors
 | 
				
			||||||
def default_socketio_error_handler(e):
 | 
					def default_socketio_error_handler(e):
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user