Skip to content

Commit

Permalink
AG-31412 Improve error message during include directives resolving.
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 9708d21
Author: jellizaveta <e.egorova@adguard.com>
Date:   Fri Dec 6 14:11:08 2024 +0300

    update changelog

commit df3b5b0
Author: jellizaveta <e.egorova@adguard.com>
Date:   Fri Dec 6 13:42:38 2024 +0300

    update CHANGELOG

commit 33ccd0c
Merge: 52af3a2 6fd1af1
Author: jellizaveta <e.egorova@adguard.com>
Date:   Wed Dec 4 19:37:20 2024 +0300

    conflicts

commit 52af3a2
Author: jellizaveta <e.egorova@adguard.com>
Date:   Wed Dec 4 19:36:34 2024 +0300

    update names and context extraction, create a separate file for logger functions

commit 6fd1af1
Author: Slava Leleka <v.leleka@adguard.com>
Date:   Wed Dec 4 13:37:05 2024 +0300

    src/filters-downloader-creator.ts edited online with Bitbucket

commit ddd9ac0
Author: jellizaveta <e.egorova@adguard.com>
Date:   Wed Dec 4 13:03:31 2024 +0300

    fix error logging

commit 47fafe8
Author: Slava Leleka <v.leleka@adguard.com>
Date:   Wed Dec 4 12:58:13 2024 +0300

    src/filters-downloader-creator.ts edited online with Bitbucket

commit bdb6e7c
Author: Slava Leleka <v.leleka@adguard.com>
Date:   Wed Dec 4 12:58:07 2024 +0300

    src/filters-downloader-creator.ts edited online with Bitbucket

commit 9a587c2
Author: Slava Leleka <v.leleka@adguard.com>
Date:   Wed Dec 4 12:57:46 2024 +0300

    src/filters-downloader-creator.ts edited online with Bitbucket

commit 514bfe6
Author: Slava Leleka <v.leleka@adguard.com>
Date:   Wed Dec 4 12:57:28 2024 +0300

    src/filters-downloader-creator.ts edited online with Bitbucket

commit 8b5f0cc
Author: jellizaveta <e.egorova@adguard.com>
Date:   Tue Dec 3 19:55:44 2024 +0300

    revert Qunit tests

commit f80bb02
Author: jellizaveta <e.egorova@adguard.com>
Date:   Tue Dec 3 19:53:36 2024 +0300

    added error message when possible

commit dc7b00a
Author: jellizaveta <e.egorova@adguard.com>
Date:   Tue Dec 3 19:10:39 2024 +0300

    error handling

commit 9ab4725
Author: Slava Leleka <v.leleka@adguard.com>
Date:   Tue Dec 3 18:20:36 2024 +0300

    __tests__/filters-downloader.test.ts edited online with Bitbucket

commit b3a10e9
Author: jellizaveta <e.egorova@adguard.com>
Date:   Tue Dec 3 18:05:26 2024 +0300

    fix error messages

commit 3c808c1
Author: jellizaveta <e.egorova@adguard.com>
Date:   Tue Dec 3 17:49:00 2024 +0300

    Fix cd '/Users/elizavetaegorova/Documents/filters-downloader'

commit eae8b8e
Author: jellizaveta <e.egorova@adguard.com>
Date:   Tue Dec 3 17:34:46 2024 +0300

    update tests

commit 569ff8f
Author: jellizaveta <e.egorova@adguard.com>
Date:   Mon Dec 2 21:03:49 2024 +0300

    fix comment

commit 073c59b
Merge: 5405295 c2cb8ab
Author: jellizaveta <e.egorova@adguard.com>
Date:   Mon Dec 2 21:03:04 2024 +0300

    Merge branch 'fix/AG-31412' of ssh://bit.int.agrd.dev:7999/extensions/filters-downloader into fix/AG-31412

commit 5405295
Author: jellizaveta <e.egorova@adguard.com>
Date:   Mon Dec 2 21:02:44 2024 +0300

    update createErrorMessage function

... and 11 more commits
  • Loading branch information
jellizaveta committed Dec 6, 2024
1 parent ff5bf21 commit 2db4570
Show file tree
Hide file tree
Showing 7 changed files with 263 additions and 39 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ module.exports = {
'no-restricted-syntax': ['error', 'LabeledStatement', 'WithStatement'],
'no-constant-condition': ['error', { checkLoops: false }],
'no-param-reassign': 'off',
'no-unused-vars': ['error', { varsIgnorePattern: '^_' }],
'no-empty': ['error', { allowEmptyCatch: false }],

'import/prefer-default-export': 'off',
'import/no-extraneous-dependencies': ['error', { devDependencies: true }],
Expand All @@ -46,6 +48,7 @@ module.exports = {
'@typescript-eslint/no-non-null-assertion': 'off',
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'error',
'@typescript-eslint/no-unused-vars': 'error',

// types described in ts
'jsdoc/require-param-type': 'off',
Expand Down
23 changes: 10 additions & 13 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.2.4] - 2024-12-06

### Changed

- Improved error message during `!#include` and condition directives resolving [FiltersCompiler#213].

[2.2.4]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.2.3...v2.2.4
[FiltersCompiler#213]: https://github.com/AdguardTeam/FiltersCompiler/issues/213

## [2.2.3] - 2024-11-25

### Added

- Ability to add filters from Google Drive [AdguardBrowserExtension#2908].

[2.2.3]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.2.2...v2.2.3
[AdguardBrowserExtension#2908]: https://github.com/AdguardTeam/AdguardBrowserExtension/issues/2908

## [2.2.2] - 2024-07-11
Expand Down Expand Up @@ -51,7 +61,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[2.1.2]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.1.1...v2.1.2
[AdguardBrowserExtension#2717]: https://github.com/AdguardTeam/AdguardBrowserExtension/issues/2717


## [2.1.1] - 2024-03-13

### Changed
Expand All @@ -60,7 +69,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

[2.1.1]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.0.7...v2.1.1


## [2.0.7] - 2024-01-17

### Changed
Expand All @@ -69,7 +77,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

[2.0.7]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.0.6...v2.0.7


## [2.0.6] - 2024-01-17

### Changed
Expand All @@ -78,7 +85,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

[2.0.6]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.0.5...v2.0.6


## [2.0.5] - 2024-01-11

### Changed
Expand All @@ -87,7 +93,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

[2.0.5]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.0.4...v2.0.5


## [2.0.4] - 2024-01-11

### Fixed
Expand All @@ -96,7 +101,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

[2.0.4]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.0.3...v2.0.4


## [2.0.3] - 2023-12-29

### Fixed
Expand All @@ -105,7 +109,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

[2.0.3]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.0.2...v2.0.3


## [2.0.2] - 2023-12-29

### Changed
Expand All @@ -114,7 +117,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

[2.0.2]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.0.1...v2.0.2


## [2.0.1] - 2023-12-29

### Changed
Expand All @@ -123,7 +125,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

[2.0.1]: https://github.com/AdguardTeam/FiltersDownloader/compare/v2.0.0...v2.0.1


## [2.0.0] - 2023-12-27

### Added
Expand All @@ -133,7 +134,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

[2.0.0]: https://github.com/AdguardTeam/FiltersDownloader/compare/v1.1.23...v2.0.0


## [1.1.23] - 2023-11-09

### Fixed
Expand All @@ -143,7 +143,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[1.1.23]: https://github.com/AdguardTeam/FiltersDownloader/compare/v1.1.21...v1.1.23
[AdguardBrowserExtension#1723]: https://github.com/AdguardTeam/AdguardBrowserExtension/issues/1723


## [1.1.21] - 2023-10-20

### Fixed
Expand All @@ -152,7 +151,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

[1.1.21]: https://github.com/AdguardTeam/FiltersDownloader/compare/v1.1.20...v1.1.21


## [1.1.20] - 2023-10-10

### Added
Expand All @@ -162,7 +160,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#20]: https://github.com/AdguardTeam/FiltersDownloader/issues/20
[1.1.20]: https://github.com/AdguardTeam/FiltersDownloader/compare/v1.1.15...v1.1.20


## [1.1.15] - 2022-11-25

### Fixed
Expand Down
122 changes: 122 additions & 0 deletions __tests__/filters-downloader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,130 @@ import nock from 'nock';

import { FiltersDownloader } from '../src';
import { server } from './server';
import { mergeErrorDetails } from '../src/helpers/logger';

const FilterCompilerConditionsConstants = {
adguard: true,
adguard_ext_chromium: true,
adguard_ext_firefox: false,
adguard_ext_edge: false,
adguard_ext_safari: false,
adguard_ext_opera: false,
adguard_ext_android_cb: false,
};

describe('FiltersDownloader', () => {
describe('error message during `!#include` and condition directives resolving', () => {
it('unexpected condition else branch', async () => {
const rules = [
'always_included_rule',
'!#else',
'if_adguard_included_rule',
];
const errorDetails = [
'Found unexpected condition else branch: \'!#else\'',
'Context:',
'\talways_included_rule',
'\t!#else',
];
expect(() => FiltersDownloader.resolveConditions(
rules,
FilterCompilerConditionsConstants,
)).toThrowError(new Error(mergeErrorDetails(errorDetails)));
});

it('unexpected condition end', async () => {
const rules = [
'always_included_rule',
'if_adguard_included_rule',
'!#endif',
];
const errorDetails = [
'Found unexpected condition end: \'!#endif\'',
'Context:',
'\talways_included_rule',
'\tif_adguard_included_rule',
'\t!#endif',
];
expect(() => FiltersDownloader.resolveConditions(
rules,
FilterCompilerConditionsConstants,
)).toThrowError(new Error(mergeErrorDetails(errorDetails)));
});

it('failed to resolve the include directive', async () => {
const nonExistentFilePath = path.resolve(__dirname, './resources/not_found_file.txt');
const rules = [
'always_included_rule',
'||example.com',
'||example.org',
`!#include ${nonExistentFilePath}`,
];
const errorDetails = [
`Failed to resolve the include directive '!#include ${nonExistentFilePath}'`,
'Context:',
'\talways_included_rule',
'\t||example.com',
'\t||example.org',
`\t!#include ${nonExistentFilePath}`,
`\tError: ENOENT: no such file or directory, open '${nonExistentFilePath}'`,
];
await expect(FiltersDownloader.resolveIncludes(
rules,
undefined,
FilterCompilerConditionsConstants,
)).rejects.toThrowError(new Error(mergeErrorDetails(errorDetails)));
});

it('failed to resolve the include directive without file path', async () => {
const rules = [
'always_included_rule',
'included_rule',
'||example.org^',
'||example.com^',
'!#include',
];
const errorDetails = [
'Failed to resolve the include directive \'!#include\'',
'Context:',
'\tincluded_rule',
'\t||example.org^',
'\t||example.com^',
'\t!#include',
'\tError: EISDIR: illegal operation on a directory, read',
];
await expect(FiltersDownloader.resolveIncludes(
rules,
undefined,
FilterCompilerConditionsConstants,
)).rejects.toThrowError(new Error(mergeErrorDetails(errorDetails)));
});

it('failed to resolve the include directive with 404 path', async () => {
const rules = [
'always_included_rule',
'||example.org^',
'!#include https://raw.githubusercontent.com/AdguardTeam/FiltersDownloader/test-resources/__test__/resources/blabla.txt',
'if_adguard_included_rule',
'||example.com^',
];
const errorDetails = [
'Failed to resolve the include directive \'!#include https://raw.githubusercontent.com/AdguardTeam/FiltersDownloader/test-resources/__test__/resources/blabla.txt\'',
'Context:',
'\talways_included_rule',
'\t||example.org^',
'\t!#include https://raw.githubusercontent.com/AdguardTeam/FiltersDownloader/test-resources/__test__/resources/blabla.txt',
'\tResponse status for url https://raw.githubusercontent.com/AdguardTeam/FiltersDownloader/test-resources/__test__/resources/blabla.txt is invalid: 404',
];
await expect(
FiltersDownloader.compile(
rules,
undefined,
FilterCompilerConditionsConstants,
),
).rejects.toThrowError(new Error(mergeErrorDetails(errorDetails)));
});
});
describe('downloadWithRaw', () => {
describe('applies patches', () => {
beforeAll(async () => {
Expand Down
17 changes: 8 additions & 9 deletions _qunit_tests_/filters-downloader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,15 +729,14 @@ QUnit.test('Test filter downloader - compile rules with conditional includes', a
'!#endif',
];

try {
await FiltersDownloader.compile(rules, null, FilterCompilerConditionsConstants);
} catch (e) {
assert.equal(
(e as Error).message,
// eslint-disable-next-line max-len
`Failed to resolve the include directive: '!#include ${URL404}'`,
);
}
assert.rejects(
FiltersDownloader.compile(rules, null, FilterCompilerConditionsConstants),
`Failed to resolve the include directive '!#include https://raw.githubusercontent.com/AdguardTeam/FiltersDownloader/test-resources/__test__/resources/blabla.txt'
Context:
always_included_rule
!#include ${URL404}
Response status for url ${URL404} is invalid: 404`,
);

// case 3: negative condition and include non-existing url
rules = [
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"scripts": {
"prebuild": "yarn rimraf dist && yarn build:types",
"build": "rollup -c rollup.config.ts --configPlugin typescript && yarn build:version",
"watch": "rollup -c rollup.config.ts --configPlugin typescript --watch",
"build:version": "ts-node scripts/build-txt.ts",
"build:types": "tsc --project tsconfig.build.json --declaration --emitDeclarationOnly --outdir dist/types",
"test": "ts-node _qunit_tests_/testrunner.ts && jest",
Expand Down
Loading

1 comment on commit 2db4570

@slavaleleka
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.