diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9b1ee42 --- /dev/null +++ b/.gitignore @@ -0,0 +1,175 @@ +# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore + +# Logs + +logs +_.log +npm-debug.log_ +yarn-debug.log* +yarn-error.log* +lerna-debug.log* +.pnpm-debug.log* + +# Caches + +.cache + +# Diagnostic reports (https://nodejs.org/api/report.html) + +report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json + +# Runtime data + +pids +_.pid +_.seed +*.pid.lock + +# Directory for instrumented libs generated by jscoverage/JSCover + +lib-cov + +# Coverage directory used by tools like istanbul + +coverage +*.lcov + +# nyc test coverage + +.nyc_output + +# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) + +.grunt + +# Bower dependency directory (https://bower.io/) + +bower_components + +# node-waf configuration + +.lock-wscript + +# Compiled binary addons (https://nodejs.org/api/addons.html) + +build/Release + +# Dependency directories + +node_modules/ +jspm_packages/ + +# Snowpack dependency directory (https://snowpack.dev/) + +web_modules/ + +# TypeScript cache + +*.tsbuildinfo + +# Optional npm cache directory + +.npm + +# Optional eslint cache + +.eslintcache + +# Optional stylelint cache + +.stylelintcache + +# Microbundle cache + +.rpt2_cache/ +.rts2_cache_cjs/ +.rts2_cache_es/ +.rts2_cache_umd/ + +# Optional REPL history + +.node_repl_history + +# Output of 'npm pack' + +*.tgz + +# Yarn Integrity file + +.yarn-integrity + +# dotenv environment variable files + +.env +.env.development.local +.env.test.local +.env.production.local +.env.local + +# parcel-bundler cache (https://parceljs.org/) + +.parcel-cache + +# Next.js build output + +.next +out + +# Nuxt.js build / generate output + +.nuxt +dist + +# Gatsby files + +# Comment in the public line in if your project uses Gatsby and not Next.js + +# https://nextjs.org/blog/next-9-1#public-directory-support + +# public + +# vuepress build output + +.vuepress/dist + +# vuepress v2.x temp and cache directory + +.temp + +# Docusaurus cache and generated files + +.docusaurus + +# Serverless directories + +.serverless/ + +# FuseBox cache + +.fusebox/ + +# DynamoDB Local files + +.dynamodb/ + +# TernJS port file + +.tern-port + +# Stores VSCode versions used for testing VSCode extensions + +.vscode-test + +# yarn v2 + +.yarn/cache +.yarn/unplugged +.yarn/build-state.yml +.yarn/install-state.gz +.pnp.* + +# IntelliJ based IDEs +.idea + +# Finder (MacOS) folder config +.DS_Store diff --git a/README.md b/README.md new file mode 100644 index 0000000..c04186c --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +# @session.js/errors + +Session.js error classes. This module can be used to check instance of thrown errors in Session.js. All errors extend from `SessionJsError` class which itself extends from generic Error class. + +```ts +import { SessionRuntimeError, SessionJsError } from '@session.js/error' + +try { + // something throws +} catch(e) { + if(e instanceof SessionRuntimeError) { + console.error('Session.js runtime error handled') + } else { + if(e instanceof SessionJsError) { + console.error('It\'s not Session.js runtime error... but at least we know it\'s Session\.js error') + } else { + console.error('No idea what is this error') + throw e + } + } +} +``` + +## Made for session.js + +Use Session messenger programmatically with [Session.js](https://github.com/sessionjs/client): Session bots, custom Session clients, and more. + +## Donate + +[hloth.dev/donate](https://hloth.dev/donate) \ No newline at end of file diff --git a/bun.lockb b/bun.lockb new file mode 100755 index 0000000..46f3363 Binary files /dev/null and b/bun.lockb differ diff --git a/package.json b/package.json new file mode 100644 index 0000000..5f4d935 --- /dev/null +++ b/package.json @@ -0,0 +1,31 @@ +{ + "name": "@session.js/errors", + "version": "1.0.0", + "module": "src/index.ts", + "main": "dist/index.js", + "types": "dist/index.d.ts", + "author": "Viktor Shchelochkov (https://hloth.dev)", + "repository": { + "type": "git", + "url": "git+https://github.com/sessionjs/errors.git" + }, + "bugs": { + "url": "https://github.com/sessionjs/errors/issues" + }, + "homepage": "https://github.com/sessionjs/errors#readme", + "description": "Session.js errors classes", + "type": "module", + "files": [ + "dist/**.js", + "dist/**.d.ts" + ], + "scripts": { + "build": "tsc --project tsconfig.build.json && tsc-alias -p tsconfig.build.json --resolve-full-paths" + }, + "peerDependencies": { + "typescript": "^5.0.0" + }, + "devDependencies": { + "tsc-alias": "^1.8.10" + } +} diff --git a/src/crypto.ts b/src/crypto.ts new file mode 100644 index 0000000..1929273 --- /dev/null +++ b/src/crypto.ts @@ -0,0 +1,20 @@ +import { SessionJsError } from "./index" + +export enum SessionCryptoErrorCode { + MessageEncryptionFailed = 'message_encryption_failed', + MessageDecryptionFailed = 'message_decryption_failed', + MessageVerificationFailed = 'message_verification_failed', +} + +/** Generic error for cases where developer does something incorrectly */ +export class SessionCryptoError extends SessionJsError { + code: SessionCryptoErrorCode + + constructor({ code, message }: { + code: SessionCryptoErrorCode, + message: string + }) { + super(message) + this.code = code + } +} \ No newline at end of file diff --git a/src/fetch.ts b/src/fetch.ts new file mode 100644 index 0000000..42425b7 --- /dev/null +++ b/src/fetch.ts @@ -0,0 +1,23 @@ +import { SessionJsError } from "./index" + +export enum SessionFetchErrorCode { + RetryWithOtherNode421Error = 'retry_with_other_node_421_error', + NoSnodesAvailable = 'no_snodes_available', + NoSwarmsAvailable = 'no_swarms_available', + FetchFailed = 'fetch_failed', + InvalidResponse = 'invalid_response', + PollingFailed = 'polling_failed', +} + +/** Generic error for cases where developer does something incorrectly */ +export class SessionFetchError extends SessionJsError { + code: SessionFetchErrorCode + + constructor({ code, message }: { + code: SessionFetchErrorCode, + message: string + }) { + super(message) + this.code = code + } +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..36b1ad7 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,10 @@ +export { SessionValidationError, SessionValidationErrorCode } from './validation' +export { SessionCryptoError, SessionCryptoErrorCode } from './crypto' +export { SessionRuntimeError, SessionRuntimeErrorCode } from './runtime' +export { SessionFetchError, SessionFetchErrorCode } from './fetch' + +export class SessionJsError extends Error { + constructor(message: string) { + super(message) + } +} \ No newline at end of file diff --git a/src/runtime.ts b/src/runtime.ts new file mode 100644 index 0000000..d819c10 --- /dev/null +++ b/src/runtime.ts @@ -0,0 +1,23 @@ +import { SessionJsError } from "./index" + +export enum SessionRuntimeErrorCode { + EmptyUser = 'empty_user', + Generic = 'generic', + NoSwarmsAvailable = 'no_swarms_available', + MultipleInstancesNotAllowed = 'multiple_instances_not_allowed', + NoInstancePolling = 'no_instance_polling', + InstanceAlreadyAuthorized = 'instance_already_authorized', +} + +/** Generic error for cases where developer does something incorrectly */ +export class SessionRuntimeError extends SessionJsError { + code: SessionRuntimeErrorCode + + constructor({ code, message }: { + code: SessionRuntimeErrorCode, + message: string + }) { + super(message) + this.code = code + } +} \ No newline at end of file diff --git a/src/validation.ts b/src/validation.ts new file mode 100644 index 0000000..00f870a --- /dev/null +++ b/src/validation.ts @@ -0,0 +1,26 @@ +import { SessionJsError } from "./index" + +export enum SessionValidationErrorCode { + InvalidMnemonic = 'invalid_mnemonic', + InvalidDisplayName = 'invalid_display_name', + InvalidSessionID = 'invalid_session_id', + InvalidPoller = 'invalid_poller', + InvalidNamespaces = 'invalid_namespaces', + NotOurPubkeyNotLegacyClosedGroup = 'not_our_pubkey_not_legacy_closed_group', + NotZeroNamespaceNotLegacyClosedGroup = 'not_zero_namespace_not_legacy_closed_group', + UnsupportedFeature = 'unsupported_feature', + InvalidMessage = 'invalid_message', +} + +/** Validation error, indicating that the developer provided invalid input */ +export class SessionValidationError extends SessionJsError { + code: SessionValidationErrorCode + + constructor({ code, message }: { + code: SessionValidationErrorCode, + message: string + }) { + super(message) + this.code = code + } +} \ No newline at end of file diff --git a/tsconfig.build.json b/tsconfig.build.json new file mode 100644 index 0000000..fbdb151 --- /dev/null +++ b/tsconfig.build.json @@ -0,0 +1,28 @@ +{ + "compilerOptions": { + // Enable latest features + "lib": ["ESNext", "DOM"], + "target": "ESNext", + "module": "ESNext", + "moduleDetection": "force", + "jsx": "react-jsx", + "allowJs": true, + "declaration": true, + + // Bundler mode + "moduleResolution": "bundler", + "verbatimModuleSyntax": true, + + // Best practices + "strict": true, + "skipLibCheck": true, + "noFallthroughCasesInSwitch": true, + + // Some stricter flags (disabled by default) + "noUnusedLocals": false, + "noUnusedParameters": false, + "noPropertyAccessFromIndexSignature": false, + "outDir": "dist", + }, + "include": ["src/index.ts"] +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..238655f --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + // Enable latest features + "lib": ["ESNext", "DOM"], + "target": "ESNext", + "module": "ESNext", + "moduleDetection": "force", + "jsx": "react-jsx", + "allowJs": true, + + // Bundler mode + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "verbatimModuleSyntax": true, + "noEmit": true, + + // Best practices + "strict": true, + "skipLibCheck": true, + "noFallthroughCasesInSwitch": true, + + // Some stricter flags (disabled by default) + "noUnusedLocals": false, + "noUnusedParameters": false, + "noPropertyAccessFromIndexSignature": false + } +}