fix: make HttpError detectable with instanceof
SPFx compiles to ES5, where subclasses of Error lose their prototype, so
`instanceof HttpError` was always false and the hub export showed the
generic message instead of "Ошибка API: {status} {statusText}".
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,9 @@
|
|||||||
|
import { HttpError } from './IHttpClient';
|
||||||
|
|
||||||
|
describe('HttpError', () => {
|
||||||
|
it('распознаётся через instanceof (сборка в ES5 ломала это без setPrototypeOf)', () => {
|
||||||
|
const error: unknown = new HttpError(403, 'Forbidden');
|
||||||
|
expect(error instanceof HttpError).toBe(true);
|
||||||
|
expect((error as HttpError).status).toEqual(403);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -31,6 +31,8 @@ export class HttpError extends Error {
|
|||||||
|
|
||||||
constructor(status: number, statusText: string, details?: string) {
|
constructor(status: number, statusText: string, details?: string) {
|
||||||
super(details ? `${status} ${statusText}: ${details}` : `${status} ${statusText}`);
|
super(details ? `${status} ${statusText}: ${details}` : `${status} ${statusText}`);
|
||||||
|
// Сборка SPFx компилирует в ES5: без этого `error instanceof HttpError` всегда false.
|
||||||
|
Object.setPrototypeOf(this, HttpError.prototype);
|
||||||
this.status = status;
|
this.status = status;
|
||||||
this.statusText = statusText;
|
this.statusText = statusText;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user