Skip to content

Commit

Permalink
feat: add version of plugin in the error title from the package.json (#…
Browse files Browse the repository at this point in the history
…141)

* feat(core): add @slangroom/core version on parse error

This should be extended to all type of errors

* refactor(core): improve @slangroom&core package version print on error

* feat: add version on error to almost all plugins

* feat(db): add plugin version in error message

* test(db): fix zencode type

* feat(ignored): add plugin version in error title

* chore: move module from node16 to esnext

now json import is working but with a warning:
(node:17829) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time
(Use  to show where the warning was created)

* fix(json-schema): bump ajv dep to 8.13.0 that now support direct use of constructor

* fix: use import instead of require to read the packge.json

---------

Co-authored-by: Puria Nafisi Azizi <[email protected]>
  • Loading branch information
matteo-cristino and puria authored May 24, 2024
1 parent 0ab23bd commit 34fc115
Show file tree
Hide file tree
Showing 46 changed files with 154 additions and 87 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"test": "pnpm build && pnpm -F @slangroom/* exec ava --verbose build/esm/test",
"coverage": "c8 -r text -r lcov -o .coverage --exclude '**/test/' pnpm test",
"clean": "rm -rf .coverage && pnpm -F @slangroom/* exec -- rm -rf build",
"build": "pnpm -F @slangroom/* exec tsc --outdir build/esm --module node16",
"build": "pnpm -F @slangroom/* exec tsc --outdir build/esm",
"publish:ci": "lerna version --no-changelog --conventional-commits --yes && pnpm publish -r --no-git-checks --filter './pkg/*'",
"docs:api": "node docs/statements/index.mjs",
"docs:ci": "node docs/statements/index.mjs ci",
Expand Down
3 changes: 2 additions & 1 deletion pkg/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"types": "./build/esm/src/*.d.ts",
"default": "./build/esm/src/*.js"
}
}
},
"./package.json": "./package.json"
},
"publishConfig": {
"access": "public"
Expand Down
4 changes: 3 additions & 1 deletion pkg/core/src/lexer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-FileCopyrightText: 2024 Dyne.org foundation
//
// SPDX-License-Identifier: AGPL-3.0-or-later
// read the version from the package.json
import packageJson from '@slangroom/core/package.json' assert { type: 'json' };

/**
* A whitespace-separated string of characters with position information.
Expand Down Expand Up @@ -87,7 +89,7 @@ export class Token {
export class LexError extends Error {
constructor(t: Token) {
super();
this.name = 'LexError';
this.name = 'LexError @slangroom/core@' + packageJson.version;
this.message = `at ${t.lineNo}:${t.start + 1}-${t.end + 1}\n unclosed single-quote \x1b[31m${t.raw}\x1b[0m`;
}
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/core/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

import { PluginMap, Token, type PluginMapKey } from '@slangroom/core';
// read the version from the package.json
import packageJson from '@slangroom/core/package.json' assert { type: 'json' };

export const errorColor = (s: string): string => '\x1b[31m' + s + '\x1b[0m';
export const suggestedColor = (s: string): string => '\x1b[32m' + s + '\x1b[0m';
Expand Down Expand Up @@ -107,7 +109,7 @@ export class ParseError extends Error {
*/
constructor(message: string) {
super(message);
this.name = 'ParseError';
this.name = 'ParseError @slangroom/core@' + packageJson.version;
}
}

Expand Down
46 changes: 24 additions & 22 deletions pkg/core/test/errors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

import { Plugin, Slangroom } from '@slangroom/core';
import test from 'ava';
// read the version from the package.json
import packageJson from '@slangroom/core/package.json' assert { type: 'json' };
// error colors
import { errorColor, suggestedColor, missingColor, extraColor } from '@slangroom/core';

Expand All @@ -27,25 +29,25 @@ Error colors:
- ${missingColor('missing words')}
- ${extraColor('extra words')}
ParseError: at 2:9-17
ParseError @slangroom/core@${packageJson.version}: at 2:9-17
${errorColor('gibberish')} may be ${suggestedColor('send')}
ParseError: at 2:9-17
ParseError @slangroom/core@${packageJson.version}: at 2:9-17
must be followed by one of: ${missingColor('param')}
ParseError: at 2
ParseError @slangroom/core@${packageJson.version}: at 2
missing one of: ${missingColor('\'<identifier>\'')}
ParseError: at 2
ParseError @slangroom/core@${packageJson.version}: at 2
missing one of: ${missingColor('and')}
ParseError: at 2
ParseError @slangroom/core@${packageJson.version}: at 2
missing one of: ${missingColor('do')}
ParseError: at 2
ParseError @slangroom/core@${packageJson.version}: at 2
missing one of: ${missingColor('some')}
ParseError: at 2
ParseError @slangroom/core@${packageJson.version}: at 2
missing one of: ${missingColor('action')}
`

Expand Down Expand Up @@ -75,7 +77,7 @@ Error colors:
- ${missingColor('missing words')}
- ${extraColor('extra words')}
LexError: at 2:20-44
LexError @slangroom/core@${packageJson.version}: at 2:20-44
unclosed single-quote ${errorColor('\'param and do some action')}
`

Expand Down Expand Up @@ -106,46 +108,46 @@ Error colors:
- ${missingColor('missing words')}
- ${extraColor('extra words')}
ParseError: at 2:1-9
ParseError @slangroom/core@${packageJson.version}: at 2:1-9
${errorColor('Gibberish')} may be ${suggestedColor('given')} or ${suggestedColor('then')}
ParseError: at 2:11-17
ParseError @slangroom/core@${packageJson.version}: at 2:11-17
${errorColor('connect')} may be ${suggestedColor('I')}
ParseError: at 2:19-20
ParseError @slangroom/core@${packageJson.version}: at 2:19-20
${errorColor('to')} may be ${suggestedColor('connect')}
ParseError: at 2:22-26
ParseError @slangroom/core@${packageJson.version}: at 2:22-26
${errorColor('\'url\'')} may be ${suggestedColor('to')}
ParseError: at 2:28-30
ParseError @slangroom/core@${packageJson.version}: at 2:28-30
${errorColor('and')} may be ${suggestedColor('\'<identifier>\'')}
ParseError: at 2:32-35
ParseError @slangroom/core@${packageJson.version}: at 2:32-35
${errorColor('send')} may be ${suggestedColor('and')}
ParseError: at 2:37-41
ParseError @slangroom/core@${packageJson.version}: at 2:37-41
${errorColor('param')} may be ${suggestedColor('send')}
ParseError: at 2:43-49
ParseError @slangroom/core@${packageJson.version}: at 2:43-49
${errorColor('\'param\'')} may be ${suggestedColor('param')}
ParseError: at 2:51-53
ParseError @slangroom/core@${packageJson.version}: at 2:51-53
${errorColor('and')} may be ${suggestedColor('\'<identifier>\'')}
ParseError: at 2:55-56
ParseError @slangroom/core@${packageJson.version}: at 2:55-56
${errorColor('do')} may be ${suggestedColor('and')}
ParseError: at 2:58-61
ParseError @slangroom/core@${packageJson.version}: at 2:58-61
${errorColor('some')} may be ${suggestedColor('do')}
ParseError: at 2:63-68
ParseError @slangroom/core@${packageJson.version}: at 2:63-68
${errorColor('action')} may be ${suggestedColor('some')}
ParseError: at 2:70-72
ParseError @slangroom/core@${packageJson.version}: at 2:70-72
${errorColor('and')} may be ${suggestedColor('action')}
ParseError: at 2:74-84
ParseError @slangroom/core@${packageJson.version}: at 2:74-84
extra token ${extraColor('aoibndwebnd')}
`

Expand Down
3 changes: 2 additions & 1 deletion pkg/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"types": "./build/esm/src/*.d.ts",
"default": "./build/esm/src/*.js"
}
}
},
"./package.json": "./package.json"
},
"publishConfig": {
"access": "public"
Expand Down
4 changes: 3 additions & 1 deletion pkg/db/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import { Plugin } from '@slangroom/core';
import { BindOrReplacements, DataTypes, Model, Sequelize } from "sequelize";
// read the version from the package.json
import packageJson from '@slangroom/db/package.json' assert { type: 'json' };

class Result extends Model {
public result!: string;
Expand All @@ -12,7 +14,7 @@ class Result extends Model {
export class DbError extends Error {
constructor(e: string) {
super(e)
this.name = 'Slangroom @slangroom/db Error'
this.name = 'Slangroom @slangroom/db@' + packageJson.version + ' Error'
}
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/db/test/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { Slangroom } from '@slangroom/core';
import { db } from '@slangroom/db';
import { DataTypes, Model, Sequelize } from 'sequelize';
import fs from "fs";
// read the version from the package.json
import packageJson from '@slangroom/db/package.json' assert { type: 'json' };

process.env['FILES_DIR'] = "./test";
const dbPath1 = "sqlite://./test/db1.db";
Expand Down Expand Up @@ -210,7 +212,7 @@ Error colors:
- missing words
- extra words
Slangroom @slangroom/db Error: [DATABASE]
Slangroom @slangroom/db@${packageJson.version} Error: [DATABASE]
Returned null for id "30" in table "firstTable" in db "sqlite://./test/db1.db".
`);
});
Expand Down Expand Up @@ -262,6 +264,6 @@ Error colors:
- missing words
- extra words
Slangroom @slangroom/db Error: [DATABASE] Database error: TypeError: Cannot read properties of null (reading 'replace')
Slangroom @slangroom/db@${packageJson.version} Error: [DATABASE] Database error: TypeError: Cannot read properties of null (reading 'replace')
`);
});
8 changes: 5 additions & 3 deletions pkg/db/test/raw_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import test from "ava";
import { Slangroom } from '@slangroom/core';
import { db } from '@slangroom/db';
import sqlite3 from "sqlite3";
// read the version from the package.json
import packageJson from '@slangroom/db/package.json' assert { type: 'json' };

const stripAnsiCodes = (str: string) => str.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');

Expand All @@ -20,7 +22,7 @@ test('Db should execute raw queries', async (t) => {
Given I have a 'string dictionary' named 'result_1'
Given I have a 'string dictionary' named 'result_2'
Given I have a 'string dictionary' named 'result_3'
Given I have a 'string dictionary' named 'result_4'
Given I have a 'string array' named 'result_4'
Given I have a 'string dictionary' named 'result_5'
Given I have a 'string dictionary' named 'result_6'
Then print all data
Expand Down Expand Up @@ -111,7 +113,7 @@ Error colors:
- missing words
- extra words
Slangroom @slangroom/db Error: SQLITE_ERROR: no such table: member
Slangroom @slangroom/db@${packageJson.version} Error: SQLITE_ERROR: no such table: member
`);
});

Expand Down Expand Up @@ -148,6 +150,6 @@ Error colors:
- missing words
- extra words
Slangroom @slangroom/db Error: SQLITE_ERROR: near "INSTERT": syntax error
Slangroom @slangroom/db@${packageJson.version} Error: SQLITE_ERROR: near "INSTERT": syntax error
`);
});
3 changes: 2 additions & 1 deletion pkg/ethereum/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"types": "./build/esm/src/*.d.ts",
"default": "./build/esm/src/*.js"
}
}
},
"./package.json": "./package.json"
},
"publishConfig": {
"access": "public"
Expand Down
4 changes: 3 additions & 1 deletion pkg/ethereum/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import { Plugin, type PluginExecutor } from '@slangroom/core';
import { erc20abi } from '@slangroom/ethereum';
import { Web3 } from 'web3';
import { isAddress } from 'web3-validator';
// read the version from the package.json
import packageJson from '@slangroom/ethereum/package.json' assert { type: 'json' };

const p = new Plugin();

export class EthereumError extends Error {
constructor(e: string) {
super(e)
this.name = 'Slangroom @slangroom/ethereum Error'
this.name = 'Slangroom @slangroom/ethereum@' + packageJson.version + ' Error'
}
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/fs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"types": "./build/cjs/src/*.d.ts",
"default": "./build/cjs/src/*.js"
}
}
},
"./package.json": "./package.json"
},
"publishConfig": {
"access": "public"
Expand Down
4 changes: 3 additions & 1 deletion pkg/fs/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import * as fspkg from 'node:fs/promises';
import * as os from 'node:os';
import axios from 'axios';
import extractZip from 'extract-zip';
// read the version from the package.json
import packageJson from '@slangroom/fs/package.json' assert { type: 'json' };

export class FsError extends Error {
constructor(e: string) {
super(e)
this.name = 'Slangroom @slangroom/fs Error'
this.name = 'Slangroom @slangroom/fs@' + packageJson.version + ' Error'
}
}

Expand Down
12 changes: 7 additions & 5 deletions pkg/fs/test/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import test from 'ava';
import { Slangroom } from '@slangroom/core';
import { fs } from '@slangroom/fs';
// read the version from the package.json
import packageJson from '@slangroom/fs/package.json' assert { type: 'json' };

const stripAnsiCodes = (str: string) => str.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');

Expand Down Expand Up @@ -37,7 +39,7 @@ Error colors:
- missing words
- extra words
Slangroom @slangroom/fs Error: $FILES_DIR must be provided
Slangroom @slangroom/fs@${packageJson.version} Error: $FILES_DIR must be provided
`);
});

Expand All @@ -62,7 +64,7 @@ Error colors:
- missing words
- extra words
Slangroom @slangroom/fs Error: ENOENT: no such file or directory, open 'test.txt'
Slangroom @slangroom/fs@${packageJson.version} Error: ENOENT: no such file or directory, open 'test.txt'
`);
});

Expand Down Expand Up @@ -105,7 +107,7 @@ Error colors:
- missing words
- extra words
Slangroom @slangroom/fs Error: path must be string
Slangroom @slangroom/fs@${packageJson.version} Error: path must be string
`);
});

Expand Down Expand Up @@ -147,7 +149,7 @@ Error colors:
- missing words
- extra words
Slangroom @slangroom/fs Error: no such file or directory: test/test_not_exist.txt
Slangroom @slangroom/fs@${packageJson.version} Error: no such file or directory: test/test_not_exist.txt
`);
});

Expand Down Expand Up @@ -179,7 +181,7 @@ Error colors:
- missing words
- extra words
Slangroom @slangroom/fs Error: file or directory found under: test/test.txt
Slangroom @slangroom/fs@${packageJson.version} Error: file or directory found under: test/test.txt
`);
const resultNotExists = slangroom.execute(verifyDoesNotExists, {
data: {
Expand Down
3 changes: 2 additions & 1 deletion pkg/git/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"types": "./build/cjs/src/*.d.ts",
"default": "./build/cjs/src/*.js"
}
}
},
"./package.json": "./package.json"
},
"publishConfig": {
"access": "public"
Expand Down
4 changes: 3 additions & 1 deletion pkg/git/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import gitpkg from 'isomorphic-git';
import http from 'isomorphic-git/http/node/index.js';
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
// read the version from the package.json
import packageJson from '@slangroom/git/package.json' assert { type: 'json' };

export class GitError extends Error {
constructor(e: string) {
super(e)
this.name = 'Slangroom @slangroom/git Error'
this.name = 'Slangroom @slangroom/git@' + packageJson.version + ' Error'
}
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"types": "./build/esm/src/*.d.ts",
"default": "./build/esm/src/*.js"
}
}
},
"./package.json": "./package.json"
},
"publishConfig": {
"access": "public"
Expand Down
Loading

0 comments on commit 34fc115

Please sign in to comment.