Skip to content

Commit

Permalink
fix: Correctly handle yggdrasil error
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Jan 3, 2024
1 parent 2cab211 commit dc77fb3
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions packages/user/yggdrasil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,22 +70,23 @@ export interface SetTextureOption {
type: 'skin' | 'cape' | 'elytra'
texture?: {
url: string
metadata?: { model?: 'slim' | 'steve'; [key: string]: any }
metadata?: { model?: 'slim' | 'steve';[key: string]: any }
} | {
data: Uint8Array
metadata?: { model?: 'slim' | 'steve'; [key: string]: any }
metadata?: { model?: 'slim' | 'steve';[key: string]: any }
}
}

export class YggdrasilError {
export class YggdrasilError extends Error {
error: string
errorMessage: string
cause?: string

constructor(o: any, readonly statusCode: number) {
this.error = o.error
this.errorMessage = o.errorMessage
this.cause = o.cause
constructor(readonly statusCode: number, message: string, o?: any) {
super(message)
this.error = o?.error
this.errorMessage = o?.errorMessage
this.cause = o?.cause
}
}

Expand Down Expand Up @@ -148,15 +149,16 @@ export class YggdrasilClient {
})

if (response.status >= 400) {
throw new YggdrasilError(await response.json(), response.status)
const body = await response.text()
throw new YggdrasilError(response.status, response.status + ':' + body, response.headers.get('content-type')?.startsWith('application/json') ? JSON.parse(body) : undefined)
}

const authentication: YggrasilAuthentication = await response.json() as YggrasilAuthentication
return authentication
}

async refresh({ accessToken, requestUser, clientToken }: { accessToken: string; clientToken: string; requestUser?: boolean }, signal?: AbortSignal) {
const response = await fetch(this.api + '/refresh', {
const response = await fetch(this.api + '/refresh', {
method: 'POST',
body: JSON.stringify({
accessToken,
Expand All @@ -172,7 +174,8 @@ export class YggdrasilClient {
})

if (response.status >= 400) {
throw new YggdrasilError(await response.json(), response.status)
const body = await response.text()
throw new YggdrasilError(response.status, response.status + ':' + body, response.headers.get('content-type')?.startsWith('application/json') ? JSON.parse(body) : undefined)
}

const authentication = await response.json() as YggrasilAuthentication
Expand Down Expand Up @@ -208,7 +211,7 @@ export interface YggdrasilTexturesInfo {
*/
export interface YggdrasilTexture {
url: string
metadata?: { model?: 'slim' | 'steve'; [key: string]: any }
metadata?: { model?: 'slim' | 'steve';[key: string]: any }
}

export function isTextureSlim(o: YggdrasilTexture) {
Expand Down Expand Up @@ -251,7 +254,8 @@ export class YggdrasilThirdPartyClient extends YggdrasilClient {
signal,
})
if (response.status !== 200) {
throw new YggdrasilError(await response.json(), response.status)
const body = await response.text()
throw new YggdrasilError(response.status, response.status + ':' + body, response.headers.get('content-type')?.startsWith('application/json') ? JSON.parse(body) : undefined)
}
const o = await response.json() as any
if (o.properties && o.properties instanceof Array) {
Expand Down Expand Up @@ -304,24 +308,24 @@ export class YggdrasilThirdPartyClient extends YggdrasilClient {
if (response.status === 401) {
if (response.headers.get('content-type') === 'application/json') {
const body = await response.json() as any
throw new YggdrasilError({
throw new YggdrasilError(response.status, response.status.toString(), {
error: body.error ?? 'Unauthorized',
errorMessage: body.errorMessage ?? 'Unauthorized',
}, response.status)
})
} else {
const body = await response.text()
throw new YggdrasilError({
throw new YggdrasilError(response.status, response.status + ':' + body, {
error: 'Unauthorized',
errorMessage: 'Unauthorized: ' + body,
}, response.status)
})
}
}
if (response.status >= 400) {
const body = await response.text()
throw new YggdrasilError({
throw new YggdrasilError(response.status, response.status + ':' + body, {
error: 'SetSkinFailed',
errorMessage: 'Fail to set skin ' + body,
}, response.status)
})
}
}
}

0 comments on commit dc77fb3

Please sign in to comment.