Skip to content

Commit

Permalink
Throw error so client can handle internally
Browse files Browse the repository at this point in the history
  • Loading branch information
andyslack committed Dec 19, 2024
1 parent b87021a commit a4c9a6a
Showing 1 changed file with 15 additions and 31 deletions.
46 changes: 15 additions & 31 deletions nuxtjs/src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default defineNuxtPlugin(({ $config }) => {
offset?: number
page?: string
sort?: string
}): Promise<ListResponse<T> | T | DeletedResponse> {
}): Promise<ListResponse<T> | T | DeletedResponse | void> {
let url: string
const fetchOptions: any = {
method: 'GET',
Expand Down Expand Up @@ -64,11 +64,7 @@ export default defineNuxtPlugin(({ $config }) => {

if (LLANA_DEBUG) console.log(`Running Llana Request: ${options.type} ${options.table} ${url}`)

try{
response = (await $fetch(LLANA_INSTANCE_URL + url, <any>fetchOptions)) as ListResponse<T>
}catch(e){
handleResponseError(e)
}
response = (await $fetch(LLANA_INSTANCE_URL + url, <any>fetchOptions)) as ListResponse<T>

break

Expand All @@ -84,11 +80,7 @@ export default defineNuxtPlugin(({ $config }) => {

if (LLANA_DEBUG) console.log(`Running Llana Request: ${options.type} ${options.table} ${url}`)

try{
response = (await $fetch(LLANA_INSTANCE_URL + url, <any>fetchOptions)) as T
} catch(e){
handleResponseError(e)
}
response = (await $fetch(LLANA_INSTANCE_URL + url, <any>fetchOptions)) as T

break

Expand All @@ -108,12 +100,7 @@ export default defineNuxtPlugin(({ $config }) => {

if (LLANA_DEBUG) console.log(`Running Llana Request: ${options.type} ${options.table} ${url}`)

try{
response = (await $fetch(LLANA_INSTANCE_URL + url, <any>fetchOptions)) as T
} catch(e){
handleResponseError(e)
}

response = (await $fetch(LLANA_INSTANCE_URL + url, <any>fetchOptions)) as T
break

case 'DELETE':
Expand All @@ -127,12 +114,7 @@ export default defineNuxtPlugin(({ $config }) => {

if (LLANA_DEBUG) console.log(`Running Llana Request: ${options.type} ${options.table} ${url}`)

try{
response = (await $fetch(LLANA_INSTANCE_URL + url, <any>fetchOptions)) as DeletedResponse
} catch(e){
handleResponseError(e)
}

response = (await $fetch(LLANA_INSTANCE_URL + url, <any>fetchOptions)) as DeletedResponse
break

case 'GET':
Expand All @@ -150,20 +132,22 @@ export default defineNuxtPlugin(({ $config }) => {

if (LLANA_DEBUG) console.log(`Running Llana Request: ${options.type} ${options.table} ${url}`)

try{
response = (await $fetch(LLANA_INSTANCE_URL + url, <any>fetchOptions)) as T
} catch(e){
handleResponseError(e)
}

response = (await $fetch(LLANA_INSTANCE_URL + url, <any>fetchOptions)) as T
break

default:
throw new Error('Invalid request type')
}

if (LLANA_DEBUG) console.dir(response)
return response
if(response.ok){
response = await response.json()
if (LLANA_DEBUG) console.dir(response)
return response
}

response.json().then((response: { error: string | undefined }) => {
handleResponseError(response.error)
})

}

Expand Down

0 comments on commit a4c9a6a

Please sign in to comment.