Skip to content

Commit

Permalink
+++
Browse files Browse the repository at this point in the history
  • Loading branch information
Offirmo committed May 20, 2024
1 parent 18de90a commit 5a140b7
Show file tree
Hide file tree
Showing 116 changed files with 738 additions and 732 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ thead {

/* TODO https://btxx.org/posts/tables/
* TODO https: //css-tricks.com/a-table-with-both-a-sticky-header-and-a-sticky-first-column/
* TODO https://github.com/paceaux/table-baseline
*/
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import {
listenToErrorEvents,
listenToUnhandledRejections,
decorateWithDetectedEnv,
getRootSEC,
getRootSXC,
} from '@offirmo-private/soft-execution-context--browser'
listenToUncaughtErrors()
listenToUnhandledRejections()
decorateWithDetectedEnv()

import { getLogger } from '@offirmo/universal-debug-api-browser'
getRootSEC().injectDependencies({ logger: getLogger({ suggestedLevel: 'silly' }) })
getRootSXC().injectDependencies({ logger: getLogger({ suggestedLevel: 'silly' }) })
```

Extra injections:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-unused-vars */
import { createLogger } from '@offirmo/practical-logger-browser'
import { getRootSEC } from '@offirmo-private/soft-execution-context'
import { getRootSXC } from '@offirmo-private/soft-execution-context'

import {
listenToErrorEvents,
Expand All @@ -9,7 +9,7 @@ import {
} from '../src/index.js'
import * as good_lib from './good_lib.js'

const APP = 'SEC_BROWSER_DEMO'
const APP = 'SXC_BROWSER_DEMO'

const logger = createLogger({
name: APP,
Expand All @@ -19,15 +19,15 @@ const logger = createLogger({
logger.notice(`Hello from ${APP}...`)


const SEC = getRootSEC()
const SXC = getRootSXC()
.setLogicalStack({
module: APP,
})
.injectDependencies({
logger,
})

SEC.emitter.on('final-error', function onError({SEC, err}) {
SXC.emitter.on('final-error', function onError({SXC, err}) {
const styles = {
error: 'color: red; font-weight: bold',
}
Expand All @@ -38,14 +38,14 @@ SEC.emitter.on('final-error', function onError({SEC, err}) {
console.log(err)
console.groupEnd()

SEC.fireAnalyticsEvent('error', {
SXC.fireAnalyticsEvent('error', {
...err.details,
message: err.message,
})
})


SEC.emitter.on('analytics', function onError({SEC, eventId, details}) {
SXC.emitter.on('analytics', function onError({SXC, eventId, details}) {
console.groupCollapsed(`⚡ Analytics! ⚡ "${eventId}"`)
console.log(`eventId: "${eventId}"`)
console.log('details', details)
Expand All @@ -57,13 +57,13 @@ listenToUnhandledRejections()
decorateWithDetectedEnv()

// Top uses tryCatch
SEC.xTry('starting', ({SEC, logger}) => {
const good_lib_inst = good_lib.create({SEC})
SEC.xTry('calling good lib', () => good_lib_inst.foo_sync({x: 1}))
SXC.xTry('starting', ({SXC, logger}) => {
const good_lib_inst = good_lib.create({SXC})
SXC.xTry('calling good lib', () => good_lib_inst.foo_sync({x: 1}))

throw new Error('Ha ha')
/*
SEC.xPromiseTry('crashing in a promise', () => {
SXC.xPromiseTry('crashing in a promise', () => {
throw new Error('Ho ho')
})*/
})
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/* eslint-disable no-unused-vars */
import { getRootSEC } from '@offirmo-private/soft-execution-context'
import { getRootSXC } from '@offirmo-private/soft-execution-context'

const LIB = 'GOOD_LIB'

function getꓽSEC(parent) {
return (parent || getRootSEC())
function getꓽSXC(parent) {
return (parent || getRootSXC())
.createChild()
.setLogicalStack({module: LIB})
}

let instance_count = 0

function create({SEC} = {}) {
function create({SXC} = {}) {
instance_count++
SEC = getꓽSEC(SEC)
SXC = getꓽSXC(SXC)

// TODO add an id?
return SEC.xTryCatch(`instantiating#${instance_count}`, ({logger, ENV}) => {
return SXC.xTryCatch(`instantiating#${instance_count}`, ({logger, ENV}) => {
// test
/*
;[
Expand All @@ -38,7 +38,7 @@ function create({SEC} = {}) {
*/

function foo_sync({x} = {}) {
SEC.xTry(foo_sync.name, () => {
SXC.xTry(foo_sync.name, () => {
if (!x) {
throw new Error('Missing arg x!') // msg will/should be auto-prefixed :-)
}
Expand All @@ -48,7 +48,7 @@ function create({SEC} = {}) {
}

async function foo_async() {
return SEC.xPromiseTry(foo_async.name, ({logger}) => {
return SXC.xPromiseTry(foo_async.name, ({logger}) => {
logger.log('attempting to do X...')
return new Promise((resolve, reject) => {
setTimeout(() => reject(new Error('failed to do X in time!')), 100) // msg will/should be auto-prefixed :-)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Bowser from 'bowser'
import { BaseInjections, SoftExecutionContext, getRootSEC } from '@offirmo-private/soft-execution-context'
import { BaseInjections, SoftExecutionContext, getRootSXC } from '@offirmo-private/soft-execution-context'
//import ensureDeviceUUID from '@offirmo-private/ensure-device-uuid-browser'

import { LS_KEYS } from './consts.js'
Expand All @@ -10,14 +10,14 @@ import { LS_KEYS } from './consts.js'
// https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror
/*
function listenToErrors() {
const SEC = getRootSEC()
const SXC = getRootSXC()
window.onerror = function (msg, url, line, colno, err) {
console.log('DEBUG', arguments)
err = err || new Error(`Error "${msg}" from "${url}", line ${line}!`)
SEC.handleError(...{
SEC,
SXC.handleError(...{
SXC,
debugId: 'browser/onError',
shouldRethrow: false,
}, err)
Expand All @@ -28,47 +28,47 @@ function listenToErrors() {
*/

function listenToErrorEvents(): void {
const SEC = getRootSEC()
const SXC = getRootSXC()
.createChild()
.setLogicalStack({operation: '(browser/on error event)'})

SEC.xTry('SEC/browser/listenToErrorEvents', ({logger}) => {
SXC.xTry('SXC/browser/listenToErrorEvents', ({logger}) => {

window.addEventListener('error', function(evt) {
// https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent
//console.log('DEBUG SEC browser debug: error event', arguments)
//console.log('DEBUG SXC browser debug: error event', arguments)
const err = (evt && evt.message === 'Script error.')
? new Error('Unreadable error from another origin!')
: evt.error || new Error(`Error "${evt.message}" from "${evt.filename}", line ${evt.lineno}.${evt.colno}!`)

SEC.handleError(err, 'browser/onError')
SXC.handleError(err, 'browser/onError')

//evt.preventDefault() // XXX should we?
})

logger.debug('Root SEC is now listening to error events ✔')
logger.debug('Root SXC is now listening to error events ✔')
})
}


function listenToUnhandledRejections(): void {
const SEC = getRootSEC()
const SXC = getRootSXC()
.createChild()
.setLogicalStack({operation: '(browser/unhandled rejection)'})

SEC.xTry('SEC/browser/listenToUnhandledRejections', ({logger}) => {
SXC.xTry('SXC/browser/listenToUnhandledRejections', ({logger}) => {

//window.onunhandledrejection = function(evt) {
window.addEventListener('unhandledrejection', function(evt) {
// https://developer.mozilla.org/en-US/docs/Web/API/PromiseRejectionEvent
//console.log('DEBUG SEC browser debug: onunhandledrejection', arguments)
//console.log('DEBUG SXC browser debug: onunhandledrejection', arguments)
//console.log(evt.reason)
const err = evt.reason || new Error('Error: uncaught promise rejection!')

SEC.handleError(err, 'browser/unhandled rejection')
SXC.handleError(err, 'browser/unhandled rejection')
})

logger.debug('Root SEC is now listening to unhandled rejection events ✔')
logger.debug('Root SXC is now listening to unhandled rejection events ✔')
})
}

Expand All @@ -80,16 +80,16 @@ interface BrowserDetails {
DEVICE_TYPE: string
}

function decorateWithDetectedEnv<Injections, AnalyticsDetails, ErrorDetails>(SEC: SoftExecutionContext<Injections, AnalyticsDetails, ErrorDetails>): void {
SEC = SEC || getRootSEC()
function decorateWithDetectedEnv<Injections, AnalyticsDetails, ErrorDetails>(SXC: SoftExecutionContext<Injections, AnalyticsDetails, ErrorDetails>): void {
SXC = SXC || getRootSXC()

const injections: Partial<BaseInjections> = {
IS_DEV_MODE: localStorage.getItem(LS_KEYS.dev_mode) === 'true',
IS_VERBOSE: localStorage.getItem(LS_KEYS.verbose) === 'true',
}

// @ts-expect-error TODO understand and fix error
SEC.injectDependencies(injections)
SXC.injectDependencies(injections)

const browser = Bowser.getParser(window.navigator.userAgent)
const details: BrowserDetails = {
Expand All @@ -103,8 +103,8 @@ function decorateWithDetectedEnv<Injections, AnalyticsDetails, ErrorDetails>(SEC
}

// @ts-expect-error TODO type properly
SEC.setAnalyticsAndErrorDetails(details)
//logger.debug('Root SEC is now decorated with env infos ✔', { Bowser: browser.getResult(), details: SEC.getAnalyticsDetails() })
SXC.setAnalyticsAndErrorDetails(details)
//logger.debug('Root SXC is now decorated with env infos ✔', { Bowser: browser.getResult(), details: SXC.getAnalyticsDetails() })
}

/////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import {
listenToUncaughtErrors,
listenToUnhandledRejections,
decorateWithDetectedEnv,
getRootSEC,
getRootSXC,
} from '@offirmo-private/soft-execution-context--node'
listenToUncaughtErrors()
listenToUnhandledRejections()
decorateWithDetectedEnv()

import { getLogger } from '@offirmo/universal-debug-api-node'
getRootSEC().injectDependencies({ logger: getLogger({ suggestedLevel: 'silly' }) })
getRootSXC().injectDependencies({ logger: getLogger({ suggestedLevel: 'silly' }) })
```

https://thecodebarbarian.com/unhandled-promise-rejections-in-node.js.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import PKG_JSON from '../package.json' assert { type: 'json' }

console.log(boxify(`
const { createLogger } = require('@offirmo/practical-logger-node')
const { getRootSEC } = require('@offirmo-private/soft-execution-context')
const { getRootSXC } = require('@offirmo-private/soft-execution-context')
const {
listenToUncaughtErrors,
listenToUnhandledRejections,
Expand All @@ -19,20 +19,20 @@ const logger = createLogger({
suggestedLevel: 'silly',
})
const SEC = getRootSEC()
const SXC = getRootSXC()
.setLogicalStack({ module: APP })
.injectDependencies({ logger })
SEC.emitter.on('final-error', function onError({logger, err}) {
SXC.emitter.on('final-error', function onError({logger, err}) {
logger.fatal('error!', {err})
})
SEC.emitter.on('analytics', function onError({SEC, eventId, details}) { … })
SXC.emitter.on('analytics', function onError({SXC, eventId, details}) { … })
listenToUncaughtErrors()
listenToUnhandledRejections()
decorateWithDetectedEnv()
// Top uses tryCatch
SEC.xTryCatch('starting', ({SEC, logger}) => { ...
SXC.xTryCatch('starting', ({SXC, logger}) => { ...
`.trim()))
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/* eslint-disable no-unused-vars */

const { createLogger } = require('@offirmo/practical-logger-node')
const { getRootSEC } = require('@offirmo-private/soft-execution-context')
const { getRootSXC } = require('@offirmo-private/soft-execution-context')

const {
listenToUncaughtErrors,
Expand All @@ -14,7 +14,7 @@ const {
} = require('../..')
const good_lib = require('./good_lib.js')

const APP = 'SEC_NODE_DEMO'
const APP = 'SXC_NODE_DEMO'

const logger = createLogger({
name: APP,
Expand All @@ -24,26 +24,26 @@ const logger = createLogger({
logger.notice(`Hello from ${APP}...`)


const SEC = getRootSEC()
const SXC = getRootSXC()
.setLogicalStack({
module: APP,
})
.injectDependencies({
logger,
})

SEC.emitter.on('final-error', function onError({SEC, err}) {
SXC.emitter.on('final-error', function onError({SXC, err}) {
logger.log('that', {err})

// or direct to reporter
SEC.fireAnalyticsEvent('error', {
SXC.fireAnalyticsEvent('error', {
...err.details,
message: err.message,
})
})


SEC.emitter.on('analytics', function onError({SEC, eventId, details}) {
SXC.emitter.on('analytics', function onError({SXC, eventId, details}) {
console.groupCollapsed(`⚡ Analytics!${eventId}`)
console.log('details', details)
console.groupEnd()
Expand All @@ -54,13 +54,13 @@ listenToUnhandledRejections()
decorateWithDetectedEnv()

// Top uses tryCatch
SEC.xTryCatch('starting', ({SEC, logger}) => {
const good_lib_inst = good_lib.create({SEC})
SEC.xTry('calling good lib', () => good_lib_inst.foo_sync({x: 1}))
SXC.xTryCatch('starting', ({SXC, logger}) => {
const good_lib_inst = good_lib.create({SXC})
SXC.xTry('calling good lib', () => good_lib_inst.foo_sync({x: 1}))

//throw new Error('Ha ha')

SEC.xPromiseTry('crashing in a promise', () => {
SXC.xPromiseTry('crashing in a promise', () => {
throw new Error('Ho ho')
})
})
Loading

0 comments on commit 5a140b7

Please sign in to comment.