I was creating custom errors in beforeError hook. After upgrading to newer got version, it's not allowed by types (but still working if to cast types).
I have to cast type.
Return type for BeforeErrorHook is any Error.
Example of casting:
hooks: { beforeError: [error => new MyCustomError(error) as any]; }
What type does it require you to use?
@sindresorhus RequestError - one what initially created by got (https://github.com/sindresorhus/got/blob/b9a855d3728d9219ddbc5d25b90d21147b982119/source/core/index.ts#L97)
So, technically it allows to add/change properties, but not change error itself.
Your custom error must inherit from RequestError. Or you can just modify the error properties which would be more efficient.
We should throw if the error we get from the beforeError hook is not an instance of RequestError.
I would love to be able to return a custom object that gets thrown. I used to do something similar with axios. But with got, every time I try to add something to the error object, it throws the unmodified error.
Our main use case for this is to expose the most useful attributes of the error as we as to not log any of the request headers so we don't leak apikeys or other things we shouldn't log. Below is the beforeError handler I would like to use, but am not able to from what I can tell.
const beforeError = ({ name, stack, code, message, response }: RequestError) => {
const { body, statusCode, retryCount } = response || {};
return {
name,
message,
stack,
code,
body,
statusCode,
retryCount
};
};
If anyone wants to see this happen in Got 12, now would be a time to submit a pull request, otherwise, we'll defer it to Got 13.
Quick update on this. I was able to get it to allow custom error objects by extending the RequestError out of got. By not passing it the full error object in the super() call and setting my own properties on it with this.whatever, all of the custom properties show up and none of the things I don't want show up.
import got, { BeforeErrorHook, RequestError } from 'got';
export class CustomRequestError extends RequestError {
public name = 'CustomRequestError';
public timings: RequestError['timings'];
public body: any;
public statusCode: number;
public retryCount: number;
public url: string;
constructor({ stack, code, message, timings, response, request }: RequestError) {
super(message, {}, null);
const { body, statusCode, retryCount } = response || {};
this.stack = stack;
this.code = code;
this.timings = timings;
this.body = body;
this.statusCode = statusCode;
this.retryCount = retryCount;
this.url = request.requestUrl;
}
}
const beforeError: BeforeErrorHook = (error) => new CustomRequestError(error);
@coleabbeduto-NM
constructor(error: RequestError) {
super(error.message, error, error.request || error.options);
Most helpful comment
I would love to be able to return a custom object that gets thrown. I used to do something similar with axios. But with got, every time I try to add something to the error object, it throws the unmodified error.
Our main use case for this is to expose the most useful attributes of the error as we as to not log any of the request headers so we don't leak apikeys or other things we shouldn't log. Below is the beforeError handler I would like to use, but am not able to from what I can tell.