Skip to content

Instantly share code, notes, and snippets.

@tmanOC
tmanOC / ExceptionUtil.java
Created September 13, 2019 14:00
Java exception utility
public class ExceptionUtil {
public interface MayThrow<R>{
public R method() throws Exception;
}
public static <T> T tryBlock(MayThrow<T> throwing) {
try{
T obj = throwing.method();
return obj;
} catch(Exception e) {
@tmanOC
tmanOC / UsingExceptionUtil.java
Created September 16, 2019 09:26
Using Exception Util
Double number = ExceptionUtil.tryBlock(() -> Double.valueOf(numberAsString))
import java.util.Optional;
public class ExceptionUtil {
public interface MayThrow<R>{
public R method() throws Exception;
}
public static <T> T tryBlock(MayThrow<T> throwing) {
try{
T obj = throwing.method();
@tmanOC
tmanOC / settings.py
Created November 19, 2019 09:15
MySQL 8 connector settings
DATABASES = {
'default': {
'ENGINE': 'mysql.connector.django',
'NAME': '****',
'USER': '****',
'PASSWORD': '****',
'HOST': '****',
'PORT': '****',
'OPTIONS': {
"use_pure": True
@tmanOC
tmanOC / methodThatReturnsAPromise.js
Created January 27, 2020 06:19
Structure of a simple promise handler
methodThatReturnsAPromise().then(result => {
handleResult(result)
}).catch(err => {
handleAnyExceptions(err)
})
@tmanOC
tmanOC / asyncAwaitVersion.js
Created January 27, 2020 06:24
async await version of a method that returns a promise
try {
var result = await methodThatReturnsAPromise()
handleResult(result)
} catch (err) {
handleAnyExceptions(err)
}
@tmanOC
tmanOC / fullPromiseHandling.js
Created January 27, 2020 06:42
Promises with async await pretty much how I like it
var err, result
[err, result] = await methodThatReturnsAPromise().then(res => [null, res], error => [error]).catch(error => {err = error})
if(err != null) {
handleAnyExceptions(err)
} else if(result != null) {
handleResult(result)
}
@tmanOC
tmanOC / kill_script_by_port.sh
Created April 22, 2020 07:15
Script that works on mac for killing scripts that use a certain port
lsof -i:$1 | awk '{print $2}' | grep -v -e "PID" | sort -u > temp.txt
pids=`cat temp.txt`
rm temp.txt
echo "The ids: $pids"
for id in $pids; do
echo "killing $id";
kill -9 $id;
done
@tmanOC
tmanOC / Output_of_lsof52639
Created April 22, 2020 07:50
lsof output
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
Code\x20H 6181 tielman 52u IPv6 0x570f09f514ea1165 0t0 TCP *:52639 (LISTEN)
@tmanOC
tmanOC / trackExceptionWrapper.js
Created June 23, 2020 06:44
Wrapper for Promise rejections of applicationinsights
appInsights = require("applicationinsights");
let exceptionTrack = appInsights.defaultClient.trackException;
appInsights.defaultClient.trackException = function myExceptionTracker(telemetry) {
let exception = telemetry.exception
//How to test for a Promise
if (Promise.resolve(exception) == exception) {
exception.then(res => {
telemetry.exception = res;
exceptionTrack.call(appInsights.defaultClient, telemetry);