Skip to content

Commit

Permalink
Expose the new-API Exception class
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 committed Dec 20, 2021
1 parent db94817 commit b6820e8
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 114 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 1.0.0-beta.7

* Expose the `Exception` class and ensure that syntax errors match the official
JS API.

## 1.0.0-beta.6

* Expose (as yet incomplete) `compile()`, `compileString()`, `compileAsync()`,
Expand Down
1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export {sassNull} from './src/value/null';
export {SassNumber} from './src/value/number';
export {SassString} from './src/value/string';

export {Exception} from './src/exception';
export {
compile,
compileString,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {Dispatcher, DispatcherHandlers} from './dispatcher';
import {MessageTransformer} from './message-transformer';
import {PacketTransformer} from './packet-transformer';
import {SyncEmbeddedCompiler} from './sync-compiler';
import {deprotofyException} from './exception';
import {Exception} from './exception';

export function compile(
path: string,
Expand Down Expand Up @@ -257,7 +257,7 @@ function handleCompileResponse(
if (sourceMap) result.sourceMap = JSON.parse(sourceMap);
return result;
} else if (response.getFailure()) {
throw deprotofyException(response.getFailure()!);
throw new Exception(response.getFailure()!);
} else {
throw Error('Compiler sent empty CompileResponse.');
}
Expand Down
64 changes: 0 additions & 64 deletions lib/src/exception.test.ts

This file was deleted.

55 changes: 11 additions & 44 deletions lib/src/exception.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,23 @@
// https://opensource.org/licenses/MIT.

import * as proto from './vendor/embedded-protocol/embedded_sass_pb';
import {SourceSpan} from './vendor/sass';
import {Exception as SassException, SourceSpan} from './vendor/sass';
import {deprotofySourceSpan} from './deprotofy-span';

/**
* An exception thrown by Sass.
*/
export class SassException extends Error {
/**
* @param message - The error message.
* @param formatted - The formatted error message. Includes the message, span,
* and trace.
* @param [span] - The source span associated with the error.
* @param [trace] - The trace associated with the error.
*/
constructor(
readonly message: string,
private readonly formatted: string,
readonly span?: SourceSpan,
readonly trace?: string
) {
super(message);
export class Exception extends Error implements SassException {
readonly sassMessage: string;
readonly sassStack: string;
readonly span: SourceSpan;

if (formatted === '') this.formatted = `Error: ${message}`;
if (trace === '') this.trace = undefined;
constructor(failure: proto.OutboundMessage.CompileResponse.CompileFailure) {
super(failure.getFormatted());

// Inject the entire Sass error into the JS stack trace.
this.stack = this.stack?.replace(
new RegExp(`^Error: ${message}`),
this.formatted
);
this.sassMessage = failure.getMessage();
this.sassStack = failure.getStackTrace();
this.span = deprotofySourceSpan(failure.getSpan()!);
}

toString() {
return this.formatted;
return this.message;
}
}

/**
* Creates a SassException from the given protocol `buffer`. Throws if the
* buffer has invalid fields.
*/
export function deprotofyException(
buffer: proto.OutboundMessage.CompileResponse.CompileFailure
): SassException {
const span = buffer.getSpan();

return new SassException(
buffer.getMessage(),
buffer.getFormatted(),
span ? deprotofySourceSpan(span) : undefined,
buffer.getStackTrace()
);
}
6 changes: 3 additions & 3 deletions lib/src/legacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import * as p from 'path';
import {URL, fileURLToPath, pathToFileURL} from 'url';

import {Exception} from './exception';
import {compileAsync, compileStringAsync} from './compile';
import {SassException} from './exception';
import {isNullOrUndefined, pathToUrlString, withoutExtension} from './utils';
import {
CompileResult,
Expand Down Expand Up @@ -141,8 +141,8 @@ function newLegacyResult(

// Decorates an Error with additional fields so that it behaves like a Node Sass
// error.
function newLegacyException(error: Error | SassException): LegacyException {
if (!(error instanceof SassException)) {
function newLegacyException(error: Error | Exception): LegacyException {
if (!(error instanceof Exception)) {
return Object.assign(error, {
formatted: error.toString(),
status: 3,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sass-embedded",
"version": "1.0.0-beta.6",
"version": "1.0.0-dev",
"protocol-version": "1.0.0-beta.11",
"compiler-version": "1.0.0-beta.9",
"description": "Node.js library that communicates with Embedded Dart Sass using the Embedded Sass protocol",
Expand Down

0 comments on commit b6820e8

Please sign in to comment.